Precreated axes with datetime ruler and time zones

11 views (last 30 days)
Dan
Dan on 12 Sep 2019
Answered: Dan on 12 Sep 2019
I would like to precreate an axes with a datetime XAxis ruler and then drop in lines with datetime objects for the 'xdata'. Everything works great until I give the line input time data a timezone. If I say the data is in 'UTC' time, I get an error:
'Cannot combine or compare a datetime array wit a time zone with one without a time zone.'
I'm guessing this in the guts of the XAxis ruler in which there are three parameters that appear to be datetime oriented. I tried to set all 3 of those parameters with the set function to datetime objects that have a time zone ('local'). But I still get the above error when attempting to add a line.
I have tried to manually set all the datetime parameters to datetime objects with no success.
How to I precreate an axes with the correct XAxis ruler to handle my datetime data that has a time zone? I think I will have to actually call the plot function to plot a line instead of calling the line function ... at least once.
% Trying to get this to work ...
% This works:
dtr = matlab.graphics.axis.decorator.DatetimeRuler;
ax = subplot(2,1,1,...
'XAxis',dtr);
drawnow % not sure why I need this ...
xdata = [datetime('yesterday'),datetime('now')]; % data does not have a time zone
line('parent',ax,'xdata',xdata,'ydata',1:2);
% This doesn't
dtr = matlab.graphics.axis.decorator.DatetimeRuler;
ax = subplot(2,1,1,...
'XAxis',dtr);
drawnow % not sure why I need this ...
xdata = [datetime('yesterday'),datetime('now')];
xdata.TimeZone = 'UTC'; % giving my data a time zone ...
line('parent',ax,'xdata',xdata,'ydata',1:2);
dtr = matlab.graphics.axis.decorator.DatetimeRuler;
ax = subplot(2,1,1,...
'XAxis',dtr);
get(dtr)
dtr.Limits.TimeZone
L = dtr.Limits
L.TimeZone = 'local'
L.TimeZone
mtv = dtr.MinorTickValues;
mtv.TimeZone = 'local'
mtv.TimeZone
tv = dtr.TickValues;
tv.TimeZone = 'local
tv.TimeZone = 'local'
set(dtr,'Limits',L,'MinorTickValues',mtv,'TickValues',tv)
set(dtr,'MinorTickValues',mtv,'TickValues',tv','Limits',L)
set(dtr,'TickValues',tv','MinorTickValues',mtv,'Limits',L)
dtr
ax
set(ax,'XTick',tv,'XLim',L)
% I'm going to have to hack it like this ... will have to realize the subplot capability myself
xdata = [datetime('yesterday'),datetime('now')];
xdata.TimeZone = 'local'
o = plot(xdata,1:2)
ax = gca;
xdata.TimeZone = 'UTC';
delete(o)
line('parent',ax,'xdata',xdata,'ydata',1:2);
  1 Comment
dpb
dpb on 12 Sep 2019
Probably worth a bug report for "quality of implementation" issue if not outright bug. Not surprising to me there are still warts in the relatively new datetime ruler object...it's not been around very long and is pretty complex.
One thing you can do is use NaN for the plotted "data" in the superfluous line so it won't ever actually be displayed--but will create the line handle.
Sometimes for such purposes I'll go ahead and deliberately pre-create the line handle(s) intending to later populate with real X/YData arrays and so having them already there may be a bonus rather than a detriment if reconsider how you're creating the end result just slightly.

Sign in to comment.

Answers (1)

Dan
Dan on 12 Sep 2019
Thanks for the feedback ... will try to write a bug report soon.
I just wanted to make a comment on how thrilled I am with the datetime ruler. WIth my hack, I was able to specify that the data going into the datetime object was 'UTC' but my axes was set to 'local' time which allowed me to see the data referenced to local time in the figure. Great to have reduced the headaches associated with dates and time. We also have to get them to display the object to higher precision ... the object supports much higher precision but won't display to that precision and I can't force it to do so ... I have to do some rounding and differencing to see what I want to see. (duration object rules here).

Community Treasure Hunt

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

Start Hunting!

Translated by