Import date data from excel

29 Ansichten (letzte 30 Tage)
Sayantan Sahu
Sayantan Sahu am 18 Jan. 2018
Beantwortet: Peter Perkins am 18 Jan. 2018
I have an excel sheet with two columns. The first column has the date in the format mmm-yy and the second column has the values.
Column A Column B Jan-05 Value Feb-05 Value Mar-05 Value
I imported the dates successfully as,
month_year=xlsread('Filename','A2:A133'); formatOut = 'mmm-yy'; month_year = datestr(month_year,formatOut)
The output month_year is of 'char' type of 132x6.
I wish to plot with x-axis being month_year and y-axis the corresponding values. So I write something like,
month_year=datenum(month_year,'mmm-yy') plot(month_year, data) datetick('x','mmmyyyy','keepticks','keeplimits')
However in the process all the dates in the x-axis are now 'Jan 00'
How do I solve this ?

Akzeptierte Antwort

Peter Perkins
Peter Perkins am 18 Jan. 2018
Unless you are using an older version of MATLAB, you are likely much better of using readtable to read your data into a table, converting the date text into a datetime (if necessary, depends on what version of MATLAB you're using and how the spreadsheet is set up), and plotting the vales vs. the datetimes.
Let's assume readtable gives you a table something like this:
>> t = table({'Jan-05';'Feb-05';'Mar-05'},[1;2;3],'VariableNames',{'A' 'B'})
t =
3×2 table
A B
________ _
'Jan-05' 1
'Feb-05' 2
'Mar-05' 3
Do this:
>> t.A = datetime(t.A,'InputFormat','MMM-yy')
t =
3×2 table
A B
___________ _
01-Jan-2005 1
01-Feb-2005 2
01-Mar-2005 3
>> plot(t.A,t.B)
If you need to tinker with the x axis ticks or labels, here's where to find them:
>> h = gca;
>> h.XAxis
ans =
DatetimeRuler with properties:
Limits: [01-Jan-2005 05-Mar-2005]
TickValues: [01-Jan-2005 15-Jan-2005 29-Jan-2005 12-Feb-2005 26-Feb-2005]
TickLabelFormat: 'MMM dd'
Show all properties

Weitere Antworten (1)

KSSV
KSSV am 18 Jan. 2018
Use this:
[num,txt,raw] = xlsread(myfile) ;
  1 Kommentar
Sayantan Sahu
Sayantan Sahu am 18 Jan. 2018
The num file imports the dates as integer numbers. In that case how would I plot it on x-axis so that it shows something like 'Jan-05'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by