Hello! I have a 339x1 cell with dates in the format (ex. : 2015-05-01, 2015-05-04), and a 339x1 double with corresponding numbers. I'm trying to figure out how to plot the data with the dates on the x axis and numbers on the Y, but it gives me an error trying to plot data from a cell. I tried using cell2mat, but that just gives me a 339x10 char which also can't be plotted. Pretty new to Matlab, would really appreciate any help!

 채택된 답변

Star Strider
Star Strider 2015년 5월 8일

6 개 추천

You need to convert the date strings to date numbers with the datenum function, plot them as date numbers, then use the datetick function to plot them as formatted dates and times.

댓글 수: 7

Ben Griffiths
Ben Griffiths 2015년 5월 8일
Thank you! How do I get the data out of a cell though and into a string so I can use it as the X axis?
My pleasure!
If it’s an array of strings such as '2015-05-01' (call it ‘datecell’ here), use datenum as:
dn = datenum(datecell, 'yyyy-mm-dd');
which should work.
Then as an example for the plot:
figure(1)
plot(dn, y)
datetick('x', 'yyyy-mm-dd')
or whatever format you want the dates in.
Ben Griffiths
Ben Griffiths 2015년 5월 8일
ahhhh amazing! it worked! thank you so much!
Star Strider
Star Strider 2015년 5월 8일
My pleasure!
Alex Backer
Alex Backer 2020년 4월 27일
What a beautiful function, thanks!
Star Strider
Star Strider 2020년 4월 27일
My pleasure!
devi r nair
devi r nair 2021년 1월 22일
편집: devi r nair 2021년 1월 22일
i have 15937 data points starting from 16-05-1975 till 31-12-2018. the data points are numbered from 0-15637. i used this code for ploting date from 16-05-1975 till 31-12-2018 but shows wrong x axis values.
start = datenum(1975,05,16);
fin=datenum(2018,12,31);
datetick('x', 'yy mmm dd', 'keepticks')
pls help what is wrong here,

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2015년 5월 13일

2 개 추천

An alternative to Star Strider's suggestion, if you are using MATLAB R2014b or later, would be
plot(datetime(datecell),y)
That's it.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2015년 5월 8일

편집:

2021년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by