Import date data from excel

53 views (last 30 days)
Sayantan Sahu
Sayantan Sahu on 18 Jan 2018
Answered: Peter Perkins on 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 ?

Accepted Answer

Peter Perkins
Peter Perkins on 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

More Answers (1)

KSSV
KSSV on 18 Jan 2018
Use this:
[num,txt,raw] = xlsread(myfile) ;
  1 Comment
Sayantan Sahu
Sayantan Sahu on 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'

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!