This program places dates on time axis as DATETICK and works with ZOOM and PAN as DATETICKZOOM by Christophe Lauwerys. But, it also shows the full date after zooming or panning by using the ticks and time-axis label. So, if you zoom in down to second, you won't loose "when" you are.
The input arguments are equal to those of DATETICK function, but if more than one axis handle is specified, they all will be LINKED!
Also, some other options are available, for example, the use of LOCAL LANGUAGE for the months (default), or the use of an specific date format: 'DD HH:MM SS.FFF' for example.
Besides, sometimes with DATETICK you don't get optimal results. This program (try to) fix this problem.
Just use it after some plot function with days (see DATENUM) in one of his axis (time-axis):
t = linspace(0,1) + now;
x = rand(size(t));
tlabel % <----
Bugs reports and suggestions will be very appreciated!
Does anyone know how to modify tlabel to make it display milliseconds when zoomed to below second resolution?
I have just re-run the test-code of my submission of 18/5/2016 on Release 2016B. It appears a few improvements have been made in the Matlab coded because:
1) The datetime plots run faster, and
2) stairs now works with datetime.
Some users may prefer to plot with the datetime class (after about R2015B I think). However, be warned that plotting with datetime is slow and is incompatible with other Matlab plotting commands. Hence, tlabel is still useful a lot of the time.
Try this example if you want to see what I mean:
tic; sd = [datenum(2017,1,1) : 1 : datenum(2018,1,1)]; toc; % Make a serial date vector
tic; sdt = datetime(sd,'ConvertFrom','datenum'); toc; % Make a DateTime array from it (slow)
ydata = sin(sd ./ 100); % Make up some data to plot
idx = 1:10:300; % Will use this later for a stairs plot
ydata2 = linspace(-0.75,0.25, 30);% Will use this later for a stairs plot
figure; tic; plot(sd, ydata); tlabel; toc; % Plot using serial date vector
figure; tic; plot(sdt, ydata); toc; % Plot using datetime array
figure; tic; plot(sd, ydata); toc; % Plot using serial date vector
hold on; stairs(sd(idx), ydata2); % Add a stairs plot - works with serial dates
figure; tic; plot(sdt, ydata); toc; % Plot using datetime array (slow!!)
hold on; stairs(sdt(idx), ydata2); % Add a stairs plot - WILL NOT WORK WITH DATETIME!!
Now, tlabel is also a bit slow but it only gets called once whereas a figure may have a lot of traces, each with a plot command (that is slow with datetime).
So, datetime has uses but so does tlabel.
Great submission, I use this routine routinely
One small issue: when panning a plot vertically, the position of the date label was not updating properly. It would re-center, but the y-position did not adjust.
I added the following code below line 353:
% Place date label 7.5% below x axis:
newYlabelPos = min(xa.YLim) - 0.075*(max(xa.YLim)-min(xa.YLim));
PLabel(2) = newYlabelPos;
Line 353 is the end of this original code:
PLabel(1*(strcmp(data.TICKAXIS,'x')) + ...
2*(strcmp(data.TICKAXIS,'y')) + ...
3*(strcmp(data.TICKAXIS,'z'))) = RLabel;
I've used tlabel for a long time in matlab, but now I'm learning python/matplotlib, I'm quite sad that there's no equivalent of tlabel! Great work, Carlos.
I have a problem with non working tlabel when opening a saved figure (.fig): the dateticks are not changing when I zoom.
I tried to load before tlabel in the matlab path but it did not helped.
Did you heard of the problem ?
All the best,
hold arrowkey -> Matlab crashes!
solved it by adding:
running = 0;
running = 1;
to the top of the tlabel function (line 198),
running = 0;
to the bottom of the tlabel function (line 303)
thanks for this great submission!
from what i've seen the datetick tool with greatest functionality.
this functionality however is payed by a slow callback.
Thanks for your file.
I am trying to use it with a plotyy. I have seen that you posted a new version on29/8/2009 for a bug with plotyy.
However, I have a problem: tlabel displays ok the ticks, but when I zoom in and out, the datetick don't refresh. What can I do?
This is what I wrote:
[AX,H1,H2] = plotyy(v1(:,1),v1(:,2),v2(:,1),v2(:,num_col));
tlabel(AX, 'x', 0, 'keepticks')
tlabel(AX(2), 'x', 0, 'keepticks')
Thanks for your help!
My mistake or Mathworks mistake for not stating so in the help file. On a Mac you need to use the option key, not shift for zooming out. Works great now!
Hi Stefan, as far as I know, that functionality of the ZOOM and PAN functions does not work even on the first version of MATLAB v7.
One workaround is to edit "temporarly" the ZOOM/PAN function by adding the line
at the end.
Or make your one function
Let me know if it works for you. Via email if possible.
tlabel.m v2.6.1 (Sep 2009)
I tried to use tlabel within a Matlab 2006a Version?
I got the following error(see below).
Is you tool compatible with older matlab versions than the 2008a stated ?
If not, do you have any suggestions on similar alternatives to datetick that run under 2006a?
??? Output argument "out" (and maybe others) not assigned during call to "D:\Programme\MATLAB\R2006a\toolbox\matlab\graph2d\zoom.m (zoom)".
Error in ==> zoom at 80
if nargin==2 && ...
Error in ==> tlabel at 242
zh = zoom(xa);
I m not able to make check this out, i did follow all the steps mentioned but im getting one error:
??? One or more output arguments not assigned during call to 'C:\matlab\MATLAB701\toolbox\matlab\graph2d\zoom.m (zoom)'.
Error in ==> tlabel at 237
zh = zoom(xa);
What do i need to do for this ?
Please let me know,
This does a great job of solving a constant Matlab plot annoyance. They really ought to pay you for it!
Worked well with the tests I ran. It seems to use good date formats when used in the default mode (i.e. just typing "tlabel;") so I will be using it in preference to "datetick;".
I've been using an older version of this function for a few months now, and it's a great little utility, much more flexible than datetick. However, I noticed two small bugs with this release:
1) If you plot data that spans several years, then zoom in to a few-months range, a reference year will appear in the ylabel. However, when you revert to the original multi-year range, the reference year ylabel remains.
2) If datetick thinks there should only be one tick, the new version of tlabel will crash on tlabel>fixTicks line 1108. This is really due to buggy tick location calculations in datetick, and may only occur in older versions of Matlab (I'm running R2007a) since I believe datetick has been updated since then. This situation only seems to arise for a small subset of cases where data spans a year plus a few hours. Older versions of tlabel were able to handle this case.
Other than those two issues, great function, and I really like the new 'whichAxes' and 'reference' options.
Bugs fixed on v2.4 Roger! Carlos
This looks very useful as I have been a bit frustrated with datetick not zooming so . I may be having trouble understanding the help though, as my testing of it didn't go very well (see script below, with comments). In particular:
1) tlabel; on its own didn't do much.
2) I couldn't feed it a numeric date format (e.g. 20) even thoughh the help says I can.
Any help with this would be appreciated as I will find this very useful.
sd = 734010:1:735000; %Test data vector
ydata = sin((sd - 734500)/73.4500); %Test data
figure; plot(sd, ydata);
datetick; %works ok but won't zoom
tlabel; %Doesn't give a sensible date format; just puts the serial date on
tlabel(20); %Does not work; script fails
Zoom-functionality makes it so much better than the built-in datetick! Thanks.
Yes, it can be done, Vincent. Let me work about it.
This has been very useful to me. Carlos, is there any way to put labels in just one of the linked axes?
v2.6.1 Fixed small bug with 'middle' reference option, thanks to Ayal Anis.
v2.6 Fixed bugs with very few ticks. Thanks to Mary-Louise Timmermans.
v2.5 Fixed bug with label date and unique tick (thanks to Kelly Kearney!)
v2.4 Fixed BUG when displaying years. Fixed BUG with numeric format (thanks to Roger Parkyn!). Added 'freeticks' and 'freelimits' options.
Fixed small typo on help (version and date)
v2.3 Fixed bug with PLOTYY. Fixed bugs on parse inputs and now the 'reference' option moves the axes label to the specified point as suggested by Giles Lesser.
v2.2 Finally fixed BUG with 'keep's options. Fixed small bug with temporal figure creation. New 'none' option for 'Reference'. Fixed small BUG with Double-Click. Other minor changes.
v2.1.2 Fixed another small bug almost in the same place as the last one.
v2.1.1 Fixed small bug on new 'keep's functionality thanks to Ayal Anis!
v2.1: 'keepticks' and 'keeplimits' default's changed accordin to ticks and limits auto/manual mode.
V2.0 Rewritten code. Mayor changes with inputs and link axes. No more outputs allowed.
Inspired by: Datetickzoom - automatically update dateticks