Code covered by the BSD License  

Highlights from
Intelligent Dynamic Date Ticks

4.83333

4.8 | 18 ratings Rate this file 102 Downloads (last 30 days) File Size: 4.86 KB File ID: #27075
image thumbnail

Intelligent Dynamic Date Ticks

by

 

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
Description

** 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.

USAGE:

dynamicDateTicks()
      makes the current axes a date axes with dynamic properties

dynamicDateTicks(axH)
      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.

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

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

Acknowledgements

This file inspired Energy Trading & Risk Management With Matlab Webinar Case Study, Electricity Load And Price Forecasting Webinar Case Study, Commodities Trading With Matlab, Matlab For R Users In Computational Finance, 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 (29)
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

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!

http://www.mathworks.com/products/matlab/whatsnew.html

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  
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
Files\MATLAB\R2012b\toolbox\matlab\graph2d\zoom.p>zoom".

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

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

01 Jun 2013 roman  
27 Mar 2013 Joerg

Thanks a lot for this tool. But the code

figH = get(axH, 'Parent');

does not always lead to the parent figure. See

http://www.mathworks.com/matlabcentral/newsreader/view_thread/31402#78934

for one possible fix for this problem.

23 Mar 2013 Matthew  
17 Mar 2013 Bert  
01 Dec 2012 Sal

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.
Thanks!

28 Mar 2012 Jirka  
07 Dec 2011 Ameya Deoras

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.

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.

Thanks

27 Aug 2011 Oleg Komarov

I suggest to change the name to something like:

datetickDynamic

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.

11 Feb 2011 Jeroen Janssens

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

08 Feb 2011 Ameya Deoras

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')

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.

Camille.

01 Nov 2010 Daniel

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.

06 Aug 2010 Shahar Barak  
01 Aug 2010 Judy

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
dynamicDateTicks

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

Thanks!!

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

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.

Updates
10 Sep 2010

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

08 Feb 2011

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

Contact us