How can I get MATLAB to plot a date on the x-axis?

조회 수: 2 (최근 30일)
Sandy
Sandy 2013년 10월 21일
댓글: dpb 2020년 1월 28일
I have a dataset with column1 = date and column2 = data. The problem is that my date is in yyyymmdd format. How can I get MATLAB to plot the date correctly in the x-axis?
Thank you in advance! :)
  댓글 수: 2
sixwwwwww
sixwwwwww 2013년 10월 21일
Do you have ur data in txt file or excel file?
Sandy
Sandy 2013년 10월 21일
It's a text file.

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

채택된 답변

dpb
dpb 2013년 10월 21일
First convert the text string date format to Matlab serial dates --
doc datenum
Then plot with that value as the x-axis value and format the dates as desired via
doc datetick
dn=datenum(data(:,1),'yyyymmdd');
plot(dn,data(:,2))
datetick('x','keepticks','keeplimits')
Salt to suit...
  댓글 수: 2
William
William 2020년 1월 28일
Thank you! This had been driving me nuts, but now works perfectly.
dpb
dpb 2020년 1월 28일
This answer is now out of date...use the new(ish) datetime class instead of datenum

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

추가 답변 (2개)

Sandy
Sandy 2013년 10월 21일
Hi dpb. I tried it this way, but for some reason it's plotting all my data as one date.
  댓글 수: 2
Sandy
Sandy 2013년 10월 21일
Nevermind, I got it. I had to convert my number to strings first. Thank you! :)
dpb
dpb 2013년 10월 21일
Oh, didn't think about the first column being integer, granted...just for the record, alternatively, you could separate out the separate fields and use the
datenum(y,m,d,h,mn,s)
form to generate the serial numbers.

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


Dainty Daisy Calgas
Dainty Daisy Calgas 2017년 12월 3일
Hello, how could I plot a quarterly data with column1, written as Q1:1991 up to Q4:2016, then my column2 is my data., please I really need help, it was in the excel.
  댓글 수: 1
dpb
dpb 2017년 12월 3일
Better form would be to create a new question, but it is at least related so I'll consider it here as "close enough"... :)
Problem first is we need to know how the date data are actually stored and imported. Since the date of the original Q, TMW has introduced the datetime class/object and improved graphics routines to be datetime aware so things are different now than then...

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

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by