File Exchange

image thumbnail

Zoom Adaptive Date Ticks

version (1.6 KB) by Andreas Krahnke
Make date ticks adapt to zooming

1 Download

Updated 01 Sep 2016

View Version History

View License

Editor's Note: This was a File Exchange Pick of the Week

Note that although this piece of code still works with R2014b, the same effect can be achieved by simply using the new datetime data type as first input to plot when running R2014b or newer versions.
% zoomAdaptiveDateTicks('on')
% Turns on the automatic adaptation of date ticks
% to user zooming for the current figure window
% zoomAdaptiveDateTicks('off')
% Turns off the automatic adaptation of date ticks
% to user zooming for the current figure window
% zoomAdaptiveDateTicks('demo')
% Opens a demo figure window to play with

Cite As

Andreas Krahnke (2021). Zoom Adaptive Date Ticks (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Amanda Zimmet

Darcy Cordell

I have a question about this script if anyone can answer that would be great.

First of all, this is a great script. However, I have a special problem. My time series is many days long (e.g. Feb 1 to Mar 15). And sometimes I need to zoom in on very specific times (e.g. 10:57 on Mar 2). However, when zooming, the date format switches from the date format I specified (yyyy-mm-dd HH:MM:SS; dateformat = 31) to simply HH:MM or to mm/yy.

Is there a way to force the function to maintain the dateformat which I specified?

Any help is appreciated.

Rafal Czarny



Very, very useful.. thank you very much

Stijn de Waele

Useful script! For multiple axes with linked time axis, I have modified the code to:
function adaptiveDateTicks(figureHandle,eventObjectHandle)
% Resetting x axis to automatic tick mark generation
arrayfun(@(x) set(x,'XTickMode','auto'),ax)
% using automaticallly generate date ticks
arrayfun(@(x) datetick(x,'x','keeplimits'),ax)

Ryan Moffet

Hans Martin Aannestad

Thank you so much for this! I have struggled with 'manual mode' and updating date vectors for ages, this is such a simple and elegant solution.

Richard Stephens

This is just what I've been looking for!

Jeremy Almeter

This function is very useful. It is short, simple, and makes the graphs easier to read.

Les Beckham

This is nice and should prove useful.

The 'demo' feature uses the 'today' function which, apparently, only ships with the Financial toolbox. If you replace the line
dates = today - linspace(1169,0,15000)';
dates = floor(now) - linspace(1169,0,15000)';

it will work without the 'today' function.

Erik Stalberg

Thanks Andreas! This tool is very useful and the code is very well written.
Erik Stalberg

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: cbdate

Community Treasure Hunt

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

Start Hunting!