Help with reading and plotting date string

조회 수: 8 (최근 30일)
Vengatesan
Vengatesan 2016년 3월 1일
댓글: Shern Wei Lee 2021년 3월 19일
I'm reading dates from an excel sheet using matlab. Matlab writes the data as '22/01/2015 13:30:00' '22/01/2015 14:00:00' '22/01/2015 14:30:00' '22/01/2015 15:00:00' '22/01/2015 15:30:00' '22/01/2015 16:00:00' '22/01/2015 16:30:00' '22/01/2015 17:00:00' '22/01/2015 17:30:00' '22/01/2015 18:00:00' '22/01/2015 18:30:00' '22/01/2015 19:00:00' '22/01/2015 19:30:00' '22/01/2015 20:00:00' '22/01/2015 20:30:00' '22/01/2015 21:00:00' '22/01/2015 21:30:00'
I would like use this date on the x-axis and to make a plot with a variable in y-axis.
Can any one help me how to make a plot with this date data. Thanks Venki

답변 (3개)

Jos (10584)
Jos (10584) 2016년 3월 1일
Convert the dates (in variable D), using DATENUM, plot, and use DATETICK to set the x-ticks to a specific format
plot(datenum(D), YVALUES)
datetick('x',31) % or any other format
  댓글 수: 2
Vengatesan
Vengatesan 2016년 3월 1일
Please see my comment to another response. Thanks.
Shern Wei Lee
Shern Wei Lee 2021년 3월 19일
Thank you

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


Florian
Florian 2016년 3월 1일
Use x = datenum( dates,'dd/mm/yyyy HH:MM:SS') to convert your strings to the internal matlab date number. Plot your x y data normally and use datetick('x',dateFormat) to format your x-axis. See http://ch.mathworks.com/help/matlab/ref/datetick.html
Regards, Florian
  댓글 수: 2
Vengatesan
Vengatesan 2016년 3월 1일
Thanks for your response. I have already tried with what you suggested before writing for help. I think the problem lies in reading the data from the excel file. I use
[D,text] = xlsread('C:\...D\...xlsx',1,'A2:B2182');
This excel file has many columns, the first column is the date and second colm is number and so on.
When I use the above function to read, the date is stored in 'text', but within the quotation as '22/01/2015 14:00:00'. This is what causing problems when the datenum function.
If I could remove the 'quotation', example 22/01/2015 14:00:00, and use the datenum function it works well. Can you let me know how to read the data without quotation. If the result look like below then i can mange
Input, date =['08/03/2015 18:00:00' '08/03/2015 18:30:00' '08/03/2015 19:00:00' '08/03/2015 19:30:00' '08/03/2015 20:00:00' '08/03/2015 20:30:00' '08/03/2015 21:00:00' '08/03/2015 21:30:00' '08/03/2015 22:00:00' '08/03/2015 22:30:00' '08/03/2015 23:00:00' '08/03/2015 23:30:00']
expected output
date =
08/03/2015 18:00:00 08/03/2015 18:30:00 08/03/2015 19:00:00 08/03/2015 19:30:00 08/03/2015 20:00:00 08/03/2015 20:30:00 08/03/2015 21:00:00 08/03/2015 21:30:00 08/03/2015 22:00:00 08/03/2015 22:30:00 08/03/2015 23:00:00 08/03/2015 23:30:00
Jos (10584)
Jos (10584) 2016년 3월 1일
Did you try? DATENUM seems to ignore quote marks
datenum('''22/02/16 13:02:34''')

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


Jan
Jan 2016년 3월 1일
You can convert the shown cell string useing datenum and use datetick to display the dates as tick lables.

카테고리

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