Plot property 'DateTimeTickFormat'

11 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
Alexandre
Alexandre on 18 Jan 2023
Thank you for your answer. This workaround might do the trick.
Just for my knowledge, is it common for Mathworks to supply patches for a specific function. I tried to browse myself in the function but I don't think it is accessible at the level I wanted.
In this case, editing two or three strings in plot function would do the trick but in the future, we may have very specific requirements from our customer and I want to know if a function revision would be a solution Mathworks could provide. It may not be free but is it doable?
Thanks again!
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!