File Exchange

image thumbnail

Intelligent Dynamic Date Ticks

version 1.5.0.1 (4.86 KB) by Ameya Deoras
Create plots with date-friendly data cursors and smart date ticks that scale with zooming & panning.

19 Downloads

Updated 01 Sep 2016

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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

Comments and Ratings (59)

Hello

very nice work
can we use this function with axes in GUI ?

thank you

Mike

Beautiful!

I was trying to get nice plots from clinical data collected over several days. This function solves all my problems!

Thank you SO MUCH!

Liu Lei

tom3w

Excellent function, very useful.

Does anyone know (or is it in the scope of a future implementation) how to remove week-end blanks related to daily financial time series, while still enjoying the benefits of this function? Xtick and Xlabel seem to only define fixed ticks/labels.

This would be even more useful compared to the current version.

Thanks!

Jithu

Prashanth

Manis Ranja

Excellent submission! Does anyone know how to make this work when x-axis is not datenum numbers. Example, I have a 3 column array. 1st column is index# like 1-100, 2nd column is date/time, 3rd column is data. I want to plot with 1st column in x-axis and 3rd column is y-axis but I want to label it with 3rd column that has dates. Reason for this is that there could be gaps in 2nd column dates/time which I don't want want to see in chart. Would anyone know what needs to be modified to make it work? Currently, it just converts 1st column to dates and labels it.

T A

Great tool. The biggest issue with this is the lack of the 'keepticks' and 'keeplimits' functionality that the original dateticks has. The 'keeplimits' one is especially frustrating, since calling xlim(...), then drawnow will not lead to dynamicDateTicks updating. Adding 'keeplimits' is a trivial effort: just add another input argument and, if it is 'keeplimits', then set the first call to datetick to include the 'keeplimit' flag. A better, but more involved solution would be to add a listener for updates to the axis limits, I think, though I've not done this myself. For 'keepticks', you'd need to modify the code a bit more.

Ali

Great work! I wondering if we can use this dynamic ticks to link a signal with its spectrogram?Thanks.

P.S. After zooming in and reset the original view is different in some data length than the original plots. This might lead to label overlapping especially when the label has the date-time. But i could solve the overlapping issue. Thanks again

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

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.

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

Alex B

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.

Pierre Quet

Thanks!

Niall Ryan

Great work! Very helpful.

Great Job!!!
Really helpful...

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?

Great - well done.

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

Shahin

Akshay Jain

Perfect !

TecKooo

wonderful job!

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!

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

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

Nakul

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

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

Oleg Komarov

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

roman

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.

Matthew

Bert

Sal

Great Tool!!!

Great tool.

Fredrik

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!

Jirka

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.

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

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.

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

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

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.

Daniel

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

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.

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

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

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" :)

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

1.5.0.1

Updated license

1.5.0.0

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

1.4.0.0

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

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor