I've plotted a file with 120,000 points. The x data is a time with a format 'MM/DD/YYYY HH:MM:SS' (10/30/2010 11:16:00 PM. I have successfully plotted it (though it took me 62 seconds to do it). My problem is when i zoom the plot, the datetick (x-axis) does automatically zooms in. In the whole plot, i only have 6 x-ticks, and when i zoom it in, the x-ticks is still the same. I've used datetick('x', 'mm/dd/yy HH:MM:SS', 'keepticks').
MATLAB: Date on X-axis when zooming the plot
datetick
Related Solutions
Hi Luc,
I see that you are utilizing the datetime datatype introduced in R2014b. MATLAB picks an optimal set of ticks and labels when you plot a datetime array.
In case - such as yours - when MATLAB's optimal set of ticks is not the most suitable, you may pan and/or zoom the plot to get your desired layout. MATLAB will automatically update the ticks and labels to match the new limits after panning/zooming.
Most importantly, only use datetick when you are plotting dates and times represented in numeric values that are serial date numbers, rather than datetime arrays.
Campion
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:
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.
Best Answer