Plot x axis in date format
이전 댓글 표시
I want to load and plot a dataset, with the x-axis in date format:

When I make my plot using the below code:
data=xlsread('XXX.xlsx');
Date=data(:,1); %week start date
Y=data(:,4);
plot(Date,Y,'ob');
But I get a strange x-axis

How can I change the x-axis to the date format in the excel? and then to a number in months?
댓글 수: 3
Mehmed Saad
2020년 4월 17일
편집: Mehmed Saad
2020년 4월 17일
it can be done by using xtick
can you attach excel file
Nora Rafael
2020년 4월 17일
Peter Perkins
2020년 4월 27일
Based on your (I guess) screenshot of an excel spreadsheet, you have a mix of text and dates in that column of the spreadsheet. That's bad news. Strongly recommend that you clean that up before doing anything. Maybe you can clean it up in MATLAB, but better to deal with the source. I have no idea what you'd even get back in the numeric output from your call to xlsread.
Once you clean that up, stay away from xlsread, and use readtable. Depending on what version of MATLAB you are using, you may get datetimes automatically, or you may get text and have to convert. If you get a cell array that is a mix of text and numbers, you haven't cleaned up the file. In very recent versions of MATLAB, use readtimetable. At that point, you can just plot vs. the datetime. Muhammad shows that, although I think the file he's using is very different than what you have.
답변 (2개)
Mehmed Saad
2020년 4월 17일
Take this as an example
T = readtable('Test.csv');
t= datetime(strrep(strrep(T.time,'T',' '),'Z',''));
figure,plot(t,T.long_E,'.')
In this example i converted the data i ve into time series so the plot is

댓글 수: 1
Mehmed Saad
2020년 4월 17일
You can convert your excel datetime usinng the following format
datetime(Date,'Format','MM/dd/yyyy')
where Date must be in the form of string
Ameer Hamza
2020년 4월 17일
0 개 추천
My answer here shows how to convert a numeric ruler on x-axis to a datetime ruler: https://www.mathworks.com/matlabcentral/answers/517403-set-the-startdate-and-enddate-of-x-axis-using-plot
Espically check the code in this comment: https://www.mathworks.com/matlabcentral/answers/517403-set-the-startdate-and-enddate-of-x-axis-using-plot#comment_827165
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!