View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Intelligent Dynamic Date Ticks

4.9 | 31 ratings Rate this file 60 Downloads (last 30 days) File Size: 4.86 KB File ID: #27075 Version:
image thumbnail

Intelligent Dynamic Date Ticks


Ameya Deoras (view profile)


24 Mar 2010 (Updated )

Create plots with date-friendly data cursors and smart date ticks that scale with zooming & panning.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

** Update: The archive now includes a new function setDateAxes which can be used like SET to set the properties of date axes (such as XLim) and have the tick labels update automatically. For example,
setDateAxes(gca, 'XLim', [datenum('July 1, 2008') datenum('August 1, 2008')])
** Update: DynamicDateTicks now supports an arbitrary day/month date formats. Use the third input argument to specify this: dynamicDateTicks([], [], 'dd/mm');

DYNAMICDATETICKS is a wrapper function around DATETICK which creates dynamic date tick labels for plots with dates on the X-axis. Features include:

* Smart ticks: Include year/month/day information on specific ticks as appropriate.
* Dynamic ticks: Ticks update as the plot is panned or zoomed
* Scaling ticks: Ticks change format as the timescale changes (from years to seconds).
* Support for multiple axes: Supports any combination of date and non-date axes as well as linked axes
* Date-friendly data tips: Dates displayed correctly on data cursors.


      makes the current axes a date axes with dynamic properties

      makes all the axes handles in vector axH dynamic date axes

dynamicDateTicks(axH, 'linked')
      additionally specifies that all the axes in axH are linked. This
      option should be used in conjunction with LINKAXES.

 dynamicDateTicks(axH, 'link', 'dd/mm')
       additionally specifies the format of all ticks that include both
       date and month information. The default value is 'mm/dd' but
       any valid date string format can be specified. The first two
       options may be empty [] if only specifying format.

load integersignal
dates = datenum('July 1, 2008'):1/24:datenum('May 11, 2009 1:00 PM');
subplot(2,1,1), plot(dates, Signal1);
subplot(2,1,2), plot(dates, Signal4);
dynamicDateTicks([], [], 'dd/mm');

ax1 = subplot(2,1,1); plot(dates, Signal1);
ax2 = subplot(2,1,2); plot(dates, Signal4);
linkaxes([ax1 ax2], 'x');
dynamicDateTicks([ax1 ax2], 'linked')


This file inspired Energy Trading & Risk Management With Matlab Webinar Case Study, Electricity Load And Price Forecasting Webinar Case Study, Matlab For R Users In Computational Finance, Commodities Trading With Matlab, Xively Read, and Cbdate.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (45)
16 Feb 2017 Jesse Hopkins

Jesse Hopkins (view profile)

@DrewVam55 and @Alex B, you can add some custom code to your figure that executes when the .fig file is loaded. See 'CreateFcn' callback (Matlab version > R2014B) or 'ResizeFcn' for older versions.

Comment only
14 Feb 2017 DrewVam55

@Alex B, I have been trying to come up with a way to fix this saving issue. The problem is that the zoom/pan mode objects are how the tick labels are controlled, but any modifications to these cannot be saved and carried over from each time you open the figure. The only way I found that works is if you overload your openfig function with a wrapper function that, after loading the figure, checks for "time" string pieces in the XTickLabels (for example, ':', '/', 'AM', 'PM'...) of the axis on the figure that is opened, then sets the pan and zoom objects callbacks to @updateDateLabel from Ameyas file. You could also store some sort of flag in the UserData of the figure before you save it, then just check for the status of that flag with your custom openfig function.

Comment only
20 Dec 2016 Steve Grobler

Really like this function, it ought to be adopted as a built-in function. Great job, thanks for making it available.

07 Dec 2016 Alex B

Alex B (view profile)

This code is very useful.

Is there any way to modify this to keep the dynamic ticks after saving/reopening the figure? They seem to get turned off after save.

Comment only
05 Oct 2016 Pierre Quet


20 Aug 2016 Niall Ryan

13 Jul 2016 Amitava Guha

Great work! Very helpful.

29 Apr 2016 Pradip Chaudhari

Great Job!!!
Really helpful...

23 Mar 2016 Vincent

Great job, really handy!

When using the function on axes in a uipanel, the function throws an error as pointed out by Juliane and Anuraag. This can easily be fixed by ensuring that figH is a handle to the figure as suggested by Ameya, not to the uipanel. Simply use figH = get(figH, 'Parent') until you get the handle of the figure. Maybe this could be altered in the original code for future use?

04 Jan 2016 Allan Kjærgaard Jensen

Great - well done.

24 Dec 2015 Javier Barro

Great job!
Thanks for thinking in 'dd/mm' format too. ;)

15 Oct 2015 Shahin

Shahin (view profile)

04 Oct 2015 Akshay Jain

Perfect !

22 Sep 2015 Fernando Souto

24 Aug 2015 Rafael Anthero

27 Feb 2015 TecKooo

wonderful job!

15 Dec 2014 Juliane

Regarding Anuraags question: Is there any workaround to be able to use dynamic date ticks inside of a uipanel?
It would be great if anyone could help!

03 Oct 2014 Ameya Deoras

Ameya Deoras (view profile)

MATLAB R2014b has built-in rotated date tick labels, as well as a datetime data type for dates. It is quite a substantial update. Check it out!

Comment only
01 Jul 2014 Aurelien Queffurust

@Anuraag : you may have this error if figH is not the handle of a figure but a handle of a uipanel for example

TO verify this : type get(figH,'Style')

13 Jun 2014 Nakul

Nakul (view profile)

23 Oct 2013 Anuraag

This is exactly what I was looking for. However, I am getting an error
Error in C:\Program Files\MATLAB\R2012b\toolbox\matlab\graph2d\zoom.p>zoom (line 232)

Output argument "out" (and maybe others) not assigned during
call to "C:\Program

Error in dynamicDateTicks (line 74)
z = zoom(figH);

I know it has been quite sometime since this function was posted but I would be really grateful if somebody could help

17 Sep 2013 Nicolai Hanssing

Excellent. Tried in vain to do this by simple commands - this solves dynamic zooming grid issue using datetick()!

25 Jun 2013 Oleg Komarov

Oleg Komarov (view profile)

If I zoom in, then undo (CTRL+Z), the axis ticks are not updated.

Comment only
01 Jun 2013 roman

roman (view profile)

27 Mar 2013 Joerg

Joerg (view profile)

Thanks a lot for this tool. But the code

figH = get(axH, 'Parent');

does not always lead to the parent figure. See

for one possible fix for this problem.

23 Mar 2013 Matthew

17 Mar 2013 Bert

Bert (view profile)

01 Dec 2012 Sal

Sal (view profile)

Great Tool!!!

31 Jul 2012 John Petersen

Great tool.

15 Jun 2012 Fredrik

15 May 2012 Rossella Blatt Vital

Great function! Works perfectly and I love the linked axes option very useful when using subplot, as well as that the dates displayed correctly on data cursor.

28 Mar 2012 Jirka

Jirka (view profile)

07 Dec 2011 Ameya Deoras

Ameya Deoras (view profile)

Jose, this function won't eliminate the dead space you see because it only modifies the ticks that would be drawn by datetick. What you need is the ability to modify the x-values in your dataset so that when they are plotted the data are no longer evenly spaced in the x-dimension. This is a much more challenging problem that this function is not set up to address.

Comment only
06 Dec 2011 Jose Antonio

I'm plotting data that is date/time stamped, I have data of six hour per day, 600 days, when my data is plotted, between one day and next I find a lot of dead space, It's like the hour I don't have the date were there. Do you know how can I eliminate those dead space? and use Intelligent Dynamic.


Comment only
27 Aug 2011 Oleg Komarov

Oleg Komarov (view profile)

I suggest to change the name to something like:


This way when I don't remember which dateticks were the "adaptive" ones I just have to write datetick and tab.

I have other datetickSOMETHING function and couldn't find immediately yours.

Comment only
11 Feb 2011 Jeroen Janssens

This is exactly what I was looking for. Great job!

08 Feb 2011 Ameya Deoras

Ameya Deoras (view profile)

Hi folks! Support for setting XLim programmatically will be here shortly. Add this file to your watch list to be notified when that update is pushed through.

For PLOTYY, you can use the following syntax. Note that zooming and panning with plotyy is limited, so making fully interactive double-axes plots may be beyond the scope of this submission:

ax = plotyy(dates1, y1, dates2, y2);
dynamicDateTicks(ax, 'linked', 'mm/dd')

Comment only
08 Feb 2011 Camille Couzi

Dear Ameya Deoras,
Thanks for creating the very usefull program dynamicDateTick.
Though I am having two problems, as:
- I am using it on plotyy, apparently your program is not compatible with this kind of plot.
- I am using xlim.
Please if you can tell me what changes to make it would be so helpful.


Comment only
01 Nov 2010 Daniel

Daniel (view profile)

Great tool, does exactly what it says on the box. Compatibility with XLim would be an awesome addition.

24 Sep 2010 Jordan Mertes

Is there any way to make it compatible with just xlim[]. So that when i specify my x axis limits it automatically readjusts the dateticks. If so this would be really great for me. But so far I have been unable to figure it out.Any advice would be great. Just a MATLAB rookie but trying. Takk.

Comment only
06 Aug 2010 Shahar Barak

01 Aug 2010 Judy

Judy (view profile)

I have MATLAB R14 SP1 and I got the error:

??? One or more output arguments not assigned during call to 'C:\MATLAB701\toolbox\matlab\graph2d\zoom.m (zoom)'.

Error in ==> dynamicDateTicks at 63
z = zoom(figH);

Error in ==> dynamicDateTicksTest at 4

when trying out the example in the given code. Is anyone familiar with known fixes needed for the zoom function?


Comment only
30 Jun 2010 Dragos Boros

The fix suggested by Thibaut works partially. When working with daily data the labels will still be reported with mm/dd format.
Trying to modify the command from line 102 by specifying a format will work but in that case the labels will only use the specified hardcoded format which will no longer vary with the data granularity

Comment only
16 Apr 2010 Thibaut Leroy

< Roger Parkyn: Change Line 128 and 129:
newlabels{1} = datestr(ticks(1), 'dd/mm/yy '); % Add month/day/year to first tick
newlabels(ind) = cellstr(datestr(ticks(ind), 'dd/m ')); % Add month/day to ticks where day changes

It's nearly perfect, it just needs a compatibility with "plotyy" :)

14 Apr 2010 Roger Parkyn

It works faster than tlabel.m (also on the file exchange, by Carlos Adrian Vargas Aguilera). However the date formats that is uses are annoying for me as an australian: we use day then month then year, or the reverse, but NEVER month/day/year.

Hence I will be sticking with tlabel until a australian option is available.

I can't fairly give it a rating as it is probably great for americans.

Comment only
10 Sep 2010 1.4

DynamicDateTicks now supports an arbitrary day/month date formats. Use the third input argument to specify this: dynamicDateTicks([], [], 'dd/mm');

08 Feb 2011 1.5

Added a function setDateAxes to set properties of an axes with dates and update tick labels automatically

01 Sep 2016

Updated license

Contact us