How do I set YAxis of Axes from a numeric ruler to a datetime ruler?

I would like to dynamically set the Yaxis to a datetime ruler.
Example Code:
figure;
aa=axes;
set(aa,'YAxis', matlab.graphics.axis.decorator.DatetimeRuler);
I get the following error:
Error
Error using matlab.graphics.axis.Axes/set
You cannot set the read-only property 'YAxis' of Axes.
I was able to set the xaxis to a datetime ruler but, the yaxis is set as read-only for some reason.

3 Kommentare

Z does not complain but gives a strange result :)
fig.png
I'm guessing this is a non-supported feature that TMW expects that if you want a datetimeruler axis that you'll create the axis with such. Doc for the axes object implies such.
Seems like there could be a time and place to be able to do this programmatically, I'd agree...submit enhancement request.
Will datetick() do?
I know, just another numeric ruler, but it does some things.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

This can be handled automatically if your Y variable is a datetime.
td = datetime(datestr(now + [0:10]'),'Format','MMM-dd');
x = 0:10;
plot(x,td)
yDateTime.png

6 Kommentare

Yes - ideally the way you listed would be best but, for my application, the ruler needs to be updated dynamically from a numeric type to a datetime type. Thank you for your input. Any work around would also we welcomed.
Why is it totally impossible to create the axes with the desired axes type? Somewhat inconvenient perhaps, but I don't see where it would be impossible by reworking how the axes object is created.
In 18b, the following command will change the ruler to DateTime
set(aa,'YRuler',matlab.graphics.axis.decorator.DatetimeRuler)
However, as soon as I plot numeric data, the ruler automatically changes back to Numeric. I haven't tested it exhaustively yet, but you may need to plot datetime data to keep your ruler datetime even if you find a workaround.
The other suggestion I might have, then, is to start by plotting a datetime like I showed up above. Capture the plot in a handle. Then, when you want to dynamically update your plot, just update the XData and YData of the line instead of replotting.
td = datetime(datestr(now + [0:10]'),'Format','MMM-dd');
x = 0:10;
h = plot(x,td);
...
h.XData = randi(100,1,10);
h.YData = randi(20,1,10);
At least from quick testing for me, this does not change the YAxis ruler.
Another undocumented method of ruler conversion to numeric, datetime, duration or categorical:
% ax is the axis handle
ax = axes();
% samp is a sample of data used to format the axis ruler
% and must be numeric, datetime, duration, categorical
% array or an objects which can be converted to double.
samp = datetime(datestr(now + [0:10]'),'Format','MMM-dd');
% To change x axis
matlab.graphics.internal.configureAxes(ax, samp, 1)
% To change y axis
matlab.graphics.internal.configureAxes(ax, 1, samp)
% To change both x and y axes
matlab.graphics.internal.configureAxes(ax, samp, samp)
And it works for other data types such as categorical
samp = categorical({'A', 'B', 'C'});
matlab.graphics.internal.configureAxes(ax, samp, 1) % x axis
matlab.graphics.internal.configureAxes(ax, 1, samp) % y axis
matlab.graphics.internal.configureAxes(ax, samp, samp) % x & y axis
And with mixed ruler types between x and y axes
samp_x = categorical({'A', 'B', 'C'});
samp_y = datetime(datestr(now + [0:10]'),'Format','MMM-dd');
matlab.graphics.internal.configureAxes(ax, samp_x, samp_y)
Unfortunately it is not possible to change the ruler of an axes. It is a read only property. The simplest and natural solution is just put a new data under the old data.
fig = figure;
ax = axes(fig);
data1 = line(ax, 1:10, linspace(datetime([2020 12 01]), datetime([2020 12 10]), 10))
ax.YAxis
% You have a datetime ruler axes.
cla
data1 = line(ax, 1:10, 1:10)
ax.YAxis
% And now you have a numerical ruler axes.
> Unfortunately it is not possible to change the ruler of an axes
Yes it is, in at least some contexts. Cris and I both share methods directly above.
To clarify your solution, cla() is the only reason it works. When you clear the axis, it's a blank slate and the axes will respond to the data classes again. If you remove cla from your answer, it no longer works. But it's a good point. Clearing the axis is a solution. But it doesn't really convert the axes. It just resets them.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by