Why does DATETICK not regenerate tick marks on the axes when using the ZOOM function in MATLAB?

11 views (last 30 days)
I am using "datetick" in my plots. If there were 5 ticks for the entire figure, and I zoom on one-fifth of the figure, I still have only one tick mark.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Apr 2023
Edited: MathWorks Support Team on 27 Mar 2023
The ability for "datetick" to regenerate tick marks on the axes when using the "zoom" function is not available in MATLAB.
As a manual workaround, try calling the "datetick" function again after you zoom in on the axes.
For more control over the precise tick mark locations, an alternative workaround is available in MATLAB releases beginning with R2009a that uses a callback function for a zoom mode object. Refer to the following steps for an example of this workaround that updates the axes with five evenly spaced tick marks each time after zooming. 
 
(1) Create and save a custom MATLAB file "zoomDateTick.m" which contains the following code:
function zoomDateTick(obj,event_obj)
nticks = 5;                             % How many tick marks to use
limits = get(event_obj.Axes,'XLim');    % Get x limits after zooming
newticks = linspace(limits(1),limits(2),nticks); % Create n ticks
set(event_obj.Axes,'XTick',newticks);   % Set x tick marks in axes
% Change format using "datetick" but preserve custom ticks: 
datetick(event_obj.Axes,'x','mmm dd HHMM','keepticks')
(2) Load or generate figure with date formatted 'x' data
(3) Return zoom mode object for figure: 
z = zoom(gcf);
(4) Assign "zoomDateTick" as the callback function to the zoom object to execute after zooming: 
set(z,'ActionPostCallback',@zoomDateTick)
(5) Zoom in or out and observe tick labels change after zooming
Another possible solution is to use a submission posted on the MATLAB File Exchange titled "Intelligent Dynamic Date Ticks", which can be found at the following link: 
https://www.mathworks.com/matlabcentral/fileexchange/27075-intelligent-dynamic-date-ticks
MathWorks does not officially support submissions hosted on the File Exchange, and so any questions regarding its use or content should be directed to the contributing author.
Alternatively, for a previous automated workaround, you can try using the Related Solution listed below.

More Answers (0)

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!