help with plotting date/time with data

4 views (last 30 days)
Vengatesan on 12 Apr 2013
Edited: per isakson on 13 Aug 2014
I'm reading some data from an excel file. The file has some heading text, so I'm loading this as,
[C,txt] = xlsread(filename);
C has 31 columns
col-1 = date,
col-2 = month,
col-3 = year,
col-4 = hour,
col-5 = min,
col-6 = sec
col-7 = mydata.
I would like to combine column 1 - 6 and write it as single 'time' as dd/mm/yyyy HH:MM:SS and plot it against 'mydata'.
My script goes, (two first lines of text removed from C)
time = [C(3:end,3) C(3:end,1) C(3:end,2) C(3:end,4) C(3:end,5) C(3:end,6)];
date = datestr(time, 'dd/mm/yyyy HH:MM:SS');
Upto here i'm getting what i need. A sample output is here:
26/03/2013 17:23:24
27/03/2013 07:13:24
27/03/2013 13:13:24
Then when I used this to plot mydata, the plot was wrong as the time axis was not correct. When i checked this with the following script just to see whether correct time is reproduced
d1 = datenum(date);
date_new =datestr(d1,'dd/mm/yyyy HH:MM:SS')
this is what i get.
03/09/0031 17:23:24
02/09/0032 07:13:24
02/09/0032 13:13:24
HH,MM and SS are correct but not the year or month or date. Workspace shows
C <4346x31 double>
d1 <4344x1 double>
date (4344x19 char>
date_new <4344x19 char>
time <4344x6 double>
Any help? Thanks.

Accepted Answer

per isakson
per isakson on 12 Apr 2013
Edited: per isakson on 13 Aug 2014
Try this:
date = '26/03/2013 17:23:24';
d1 = datenum(date);
date_new =datestr(d1,'dd/mm/yyyy HH:MM:SS')
date = '26/03/2013 17:23:24';
d1 = datenum( date, 'dd/mm/yyyy HH:MM:SS' );
date_new =datestr(d1,'dd/mm/yyyy HH:MM:SS')
I get (R2012a)
date_new =
03/09/0031 17:23:24
date_new =
26/03/2013 17:23:24
Doc says:
DateNumber = datenum(DateString) converts date string DateString into
a serial date number. String DateString must be in one of the date
formats 0, 1, 2, 6, 13, 14, 15, 16, or 23, as defined in the reference
page for the datestr function.
I assume that '26/03/2013 17:23:24' is not according to one of these formats ( 0, 1, 2, 6, 13, 14, 15, 16, or 23).
Be careful with "unknown" default values.
  1 Comment
Vengatesan on 13 Apr 2013
Many thanks for this solution. works well and produces the plot.

Sign in to comment.

More Answers (1)

KRUNAL on 12 Aug 2014
Edited: KRUNAL on 12 Aug 2014
Can you post your script u wrote for plotting your 'mydata' against time. I am having some errors in a code that works on similar principle.
I have date in column 1 and time in column 2 and data in column 3. I want to plot date/time against that data. Can anyone tell me how can I do it?

Community Treasure Hunt

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

Start Hunting!