Why do I get the error "Unable to set the 'YAxis' property of class UIAxes because it is read-only" when I programmatically assign a new y-axis ruler in MATLAB R2024a?

I have been programmatically changing the type of axis by assigning a new ruler of the correct type. For example, for the y-axis:
a = uiaxes;
a.YAxis = matlab.graphics.axis.decorator.DurationRuler;
I then get the following error:
Unable to set the 'YAxis' property of class UIAxes because it is read-only.
However, I do not receive any error when I do this with the x-axis.
Is this a bug? Why is the behaviour between programmatically assigning a new ruler different for the y-axis and the x-axis? What are the workarounds for this error?

 Accepted Answer

This is not a bug. 
The differing behaviour you experienced between the x and y-axes is due to how they were implemented.
In particular, setting the axes to a different type of ruler was not intended to be a documented workflow. The only officially documented way to change a ruler to a different type is to call a command (such as "yline") that supports duration data. Therefore, a suitable workaround would be:
a = uiaxes;
% Plot a dummy vertical line at a duration value to force the X axis to become a DurationRuler
yline(a, seconds(0)); % This will change the X axis to a duration type
delete(a.Children); % Remove the dummy line if not needed

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Release

R2024a

Community Treasure Hunt

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

Start Hunting!