How do I control the format of the date stamp on a plot axis with datetime values?

9 Ansichten (letzte 30 Tage)
Hi,
I have some data with timestamps, so I have plotted values on the y-axis versus a datetime array on the x-axis.
I really like that Matlab summarized the date for me, since all the ticks have the date in common, only a different time. Similarly, it will summarize the year if all ticks have this in common. However, how do I control the format of this extra label on the axis, in this case the date? I can control the format of the xticks, but that is not what I'm looking for. I would like to display the date differently (eg 'd MMMM yyyy'), instead of the 'MMMM d, yyyy' that it is now.

Akzeptierte Antwort

Stephen23
Stephen23 am 13 Feb. 2024
Bearbeitet: Stephen23 am 13 Feb. 2024
That is named the secondary label and has some rather rudimentary controls, e.g.:
Unfortunately to control its format you will need to dig into some undocumented features... and (AFAIK) we will have to modify the text after the plot command. Lets try now:
X = datetime(2024,2,13,0:23,0,0);
Y = rand(1,numel(X));
plot(X,Y)
A = gca;
drawnow() % the forum requires this
S = A.XRuler.SecondaryLabel.String
S = 'Feb 13, 2024 '
D = datetime(S, 'InputFormat','MMM d, y', 'Format','d MMMM yyyy')
D = datetime
13 February 2024
A.XRuler.SecondaryLabel.String = char(D);
hold on
plot(0,0) % required to update the forum plot
See also:
Tip: go to the xSecondaryLabel page (linked at the top of my answer), scroll to the very bottom, give a rating (of your choice) together with the feedback that the function needs to include the ability to change the date format.
  1 Kommentar
Iris
Iris am 13 Feb. 2024
Perfect, thank you!
I will definitely follow up on your tip. Hopefully there will then be a more straightforward functionality of xSecondaryLabel in the future :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by