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.

 Réponse acceptée

Cris LaPierre
Cris LaPierre le 18 Mar 2020
Modifié(e) : Cris LaPierre le 18 Mar 2020
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 commentaires

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.
Cris LaPierre
Cris LaPierre le 19 Mar 2020
Modifié(e) : Cris LaPierre le 19 Mar 2020
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.
Thanks. The answer is in this post.
Dear Cris, I really appreciate. I could fixed it
figure
pcolor(datenum(Time),-P,S);shading flat;colorbar
datetick('x', 'yyyy-mmm', 'keepticks')
(thanks)^10000000
Very good! Glad you got it working.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by