Plot property 'DateTimeTickFormat'

10 views (last 30 days)
Alexandre
Alexandre on 14 Jan 2023
Commented: Adam Danz on 18 Jan 2023
Hi,
I've just discovered this property of plot function and I set a Format but I noticed some differences compared to default plot when no 'DateTimeTickFormat' is specified.
plot1Objs(2) = plot(app.plantActivePowerUIAxes, t, yPLANTMW);
plot1Objs(2) = plot(app.plantActivePowerUIAxes, t, yPLANTMW,'DateTimeTickFormat','yyyy-MMM-dd hh:mm a');
In default mode, the date only appears once if the range fits in a single day and minutes and seconds appears if I zoom in, which I really like. If I specify my own Format , the plot does not adjust as I zoom and I end up with 5 identical ticks. Is there a way to enter the Format so it behaves the same way? I couldn't figure it out with the documentation.
Also, the cursor still shows the default plot function datetime Format . Is there anything I can do about it?
A patch directly in the plot function would be an acceptable solution as well since the format I choose will never change for my application.
Thank you!
  1 Comment
Alexandre
Alexandre on 17 Jan 2023
I assume all answers to my question are no but I'll give it a second try. I'd like to know why "no" though.
Thanks.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 17 Jan 2023
When tick label format is not specified, datetime tick labels are automatically selected based on the range of your data and the format of the datetime values. When you zoom into your data, the datetime range changes and the auto tick labels are updated as needed.
When you specify a tick label format, the auto features are turned off. You're essentially telling MATLAB that you want to format the labels so MATLAB stays out of your way. The automatic adjustment of tick label formats are turned off so zooming does not update the label format.
Workaround
You could detect when the axis range changes using the LimitsChangedFcn which is evoked when the axis limits changed (e.g. when zooming; see Community Highlight). It might take some fine tuning but the function would update the tick label format following a set of rules.
In this demo, the tick labels are set to auto when the range of the datetime axes is less than 2 minutes or there are less than 2 ticks. Otherwise, it returns the tick format to a fixed format.
d = datetime(2023,1,17,23,59,0) + minutes(0:0.1:150);
y = hours(d-d(1)).^2 + .15*rand(size(d));
h = plot(d,y,'DateTimeTickFormat','yyyy-MMM-dd hh:mm a');
axis tight
ax = gca;
ax.XAxis.LimitsChangedFcn = @XLimitsChangeFcn;
function XLimitsChangeFcn(ruler,~)
limits = ruler.Limits;
limitRange = diff(limits);
nTicks = numel(ruler.TickValues);
% Rule: change to auto when the datetime axis range
% is less than 2 minutes or when there are less than
% 2 datetime ticks.
if limitRange < minutes(2) || nTicks < 2
ruler.TickLabelFormatMode = 'auto';
else
ruler.TickLabelFormat = 'yyyy-MMM-dd hh:mm a';
end
end
  2 Comments
Adam Danz
Adam Danz on 18 Jan 2023
To address that question, contact tech support to create a service request.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!