2
votes

I'm using Matlab R2015a, and would like to control the DatetimeTickFormat when plotting with the plotyy function. However, plotyy does not take that argument, in contrast to the regular plot function.

How can I access and set the DatetimeTickFormat via a handle or command, after plotting with plotyy?

x_datenum = linspace(1,2,10);
t_datetime = datetime(x_datenum,'ConvertFrom', 'datenum');

figure
% WORKS, BUT GIVES NO CONTROL OVER THE 'DatetimeTickFormat'
[hAxPlotyy,hLine1,hLine2] = plotyy(t_datetime,1:10,t_datetime,2:11);

% DOES NOT WORK. HOW DO I SET A SPECIFIC 'DatetimeTickFormat' AFTERWARDS?
[hAxPlotyy,hLine1,hLine2] = plotyy(hAx,t_datetime,1:10,t_datetime,2:11,'DatetimeTickFormat','HH:mm:ss');

Thank you.

1
does hAxPlotyy.DatetimeTickFormat work? what about gca.DatetimeTickFormat? - Ander Biguri
None of those work, but thanks. I also tried hAxPlotyy.XTickFormat and hAxPlotyy.TickFormat. I have been looking at all the properties for hAxPlotyy in the Inspector window, but I have yet to find a property that seems to work for this issue. EDIT: Actually there are no XTickFormat or TickFormat properties for my hAxPlotyy handle object, from what I see in the Inspector, but I simply tried those before looking at the Inspector. - ctp
Its weird because if you change the font of the axes, sometimes, the format changes. I got something like 20072015 instead of 20-jun-2015 and so on.... - Ander Biguri
I'm not sure if it would help in this situation (still booting up my machine) but I generally try to avoid plotyy due to how fussy it's been for me historically. I've moved to just stacking axes and avoiding it altogether (one example here). - excaza
Stepping through plot in the debugger it doesn't seem like DatetimeTickFormat is actually a property of the axes or plot, but instead a different version of plot is called that utilizes the internal matlab.internal.datetime.makeTimePlot function. - excaza

1 Answers

1
votes

It seems like I went a bit too far down the plot rabbit hole without checking for a dedicated function first. Oops...

You can use datetime after your plotyy call to modify the ticks:

x_datenum = linspace(1,2,10);
t_datetime = datetime(x_datenum,'ConvertFrom', 'datenum');

[hAxPlotyy,hLine1,hLine2] = plotyy(t_datetime,1:10,t_datetime,2:11);
datetick(hAxPlotyy(1), 'x', 'HH:mm:ss');

Since hAxPlotyy returns a 1x2 array of axes objects and datetime apparently doesn't work on arrays of axes objects, I selected the primary axes object to modify.

Modified X-axis ticks