help with plotting date/time with data

조회 수: 2 (최근 30일)
Vengatesan
Vengatesan 2013년 4월 12일
편집: per isakson 2014년 8월 13일
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.

채택된 답변

per isakson
per isakson 2013년 4월 12일
편집: per isakson 2014년 8월 13일
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
Vengatesan
Vengatesan 2013년 4월 13일
Many thanks for this solution. works well and produces the plot.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KRUNAL
KRUNAL 2014년 8월 12일
편집: KRUNAL 2014년 8월 12일
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?

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by