Axes not reconizing figure handle

5 views (last 30 days)
Gabriel Stanley
Gabriel Stanley on 7 Oct 2022
Commented: dpb on 8 Oct 2022
So I'm trying to setup a plotting function that will not overwrite any existing plots, and to predefine the values of certain properties of interest. However, for reasons I do not understand (but I think are actually obvious), using axes seems to change how it handles the Parent argument based on what other arguments I'm passing.
Plot = figure(1);
Axis = axes(Plot);
works fine, whereas
Plot = figure(1);
Axis = axes(Plot,'Position',[0.1300 0.3300 0.7750 0.700],'XLabel','SomeText','YLabel','MoreText',...
throws the error:
"Error using axes
Value must be handle."
My understanding is that Plot as I've defined it is a handle, and without trying to set any axis properties axes seems to accept it as such. But if I try to set any other axis properties, suddenly axes doesn't recognize the handle?
dpb on 7 Oct 2022
I would also recommend against such Long_And_Drawn_Out_VariableNamesThatAreAlsoAlmostAlikeUntil_THEVERYEND
Not only are they a lot of typing for nothing, they simply obfuscate reading code for meaning and being able to tell what is going on easily. They are far more detrimental than helpful; if comments are needed to explain something, then use then out of the way of the straight line of the code itself.

Sign in to comment.

Accepted Answer

dpb on 7 Oct 2022
hAx=axes(hF,'Position',[0.1300 0.3300 0.7750 0.700]);
xlim(hAx,[0 300])
ylim(hAx,[0 100])
Of the given properties, only 'Position' is a property of the axes settable on creation.
The X/Y labels are text objects that are children of the axes; X/Y limits are properties also of the axes...
See axes for the allowed Name-Value Pair Arguments, only the position-related ones are accepted on creation; everything else has to have the axis handle as parent or, in the case of the axis labels, they are sub-objects in their own right and have their own properties. Even xlabel is a higher-level interface to the actual object itself, it's its 'String' property that actually holds the display string data.
dpb on 8 Oct 2022
Can be a little more succinct in setting multiple properties on a given object (or collection of objects); the above could be written as
hAx=axes(hF,'Position',[0.1300 0.3300 0.7750 0.700]);
axis(hAx,[0 300 0 100]) % axis() sets limits, style, ydirection, ...
NOTA BENE: the vector of object handles to the axis labels and then the 'String' property of those.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 7 Oct 2022
The problem seems to be specifically with the XLabel argument, and the error message is non-intuitive (at least to me).
This works:
Plot = figure(1);
Axis = axes(Plot,'Position',[0.1300 0.3300 0.7750 0.700],'XLim',[0 300],'YLim',[0 100]);
Not sure if this is a bug, or something I just don't fully understand. A work-around is to add the labels later, using the xlabel function.
Gabriel Stanley
Gabriel Stanley on 7 Oct 2022
Hunh, weird. I agree with your preception of the error as non-inuitive, given that I didn't think to perform 1-by-1 argument elimination specifically because the returned error implicated the Plot object handle.
That said, with you pointing me to the XLabel argument, I think I know what's going on: the [X/Y/Z]Label arguments are themselves objects with their own properties. The functions which bear the same name directly modify the String property of the indicated object, hence the documentation's example of
ax = gca;
ax.YLabel.String = 'My y-Axis Label';
The text object also has it's own Position property, such that the syntax
Axis = axes(Plot,'XLabel',text('String','YourTextHere'));
automatically creates 2 axes, 1 of which is unlabeled. Not sure it's worth the time to figure out how to attach the text object the the base axis inline.
Thank you for your help.

Sign in to comment.


Find more on Labels and Annotations in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by