how to plot the time series data with dates showing in x axis

I can plot time series but I am not able to show exact dates on x axis. I really appreciate anybody can help me. Thanks
figure
Ts = timeseries(Ta);
plot(Ts,'r')
grid on
grid minor
xlabel('Time','fontsize',14)
ylabel('Temperature (\circC)','fontsize',14)
Problem, cannot manage date lable, you can find attached file may show me how it's work.

 Accepted Answer

Convert your dates to the datetime data type. Then, when you plot using the datetime, the date will appear on the axis. You can set the display format of a datetime to show the date however you'd like.
load example.mat
Time = datetime(time,'ConvertFrom',"excel")
plot(Time,Ta)
You may need to adjust the ConvertFrom datatype. I didn't know, so I just selected a common one.

14 Comments

Dear Cris, tnx
it should be ....,'excel')
Dear Cris,
Years has been displayed form of 0066-May, 0066-Jun,0067-Jan,...etc. I would to be 2016-May, 2016-Jun, ...etc. Year reference is 1950. You know how can I do it?
Interesting. The code I shared imports the year as 1966, 1967, etc for me.
For me, your vector time appears to be in units of days. So if the reference data is 01-01-1950, the first date is around 12-06-2016. Does that sound correct?
If your time does not correspond to a built-in option, you can specity your own epoch. However, this appears to expect the time values to be in seconds, not days. You can modify the datetime conversion to the following to account for this:
% Convert time from days to seconds for conversion. Reference is 1950
Time = datetime(time*60*60*24,'ConvertFrom','epochtime','Epoch','1950-01-01')
Dear Cris,
I really appreciate your patience, Time reference is "1950-01-01T00:00:00Z". I want to be in format "mmm-yyyy", and also "dd-mmm-yyyy" , thanks.
Take a look at the documentation. The information you want is there.
Dear Cris, I fixed the date, proble is plotting. Could you tell me how can I fixed it. Thanks
>> pcolor(Time,-P,T);shading flat;colorbar; colormap(jet)
Error using pcolor (line 63)
Data inputs must be real.
Formatting of datetime tick labels on the X-axis can be done with the function xtickformat.
I don't know what P and T are, so I can't reproduce your error. Perhaps this post can help with the error you are seeing? Especially if you are using MATLAB r2019a or older.
Dear Cris, I attached P(pressure) and T(temperature). I really appreciate your help. Thanks.
And what version of MATLAB are you using? If 2019a or earlier, look at the two posts I've already shared that show you how to fix this.
Dear Cris, I really appreciate. I could fixed it
figure
pcolor(datenum(Time),-P,S);shading flat;colorbar
datetick('x', 'yyyy-mmm', 'keepticks')
Very good! Glad you got it working.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!