Axes not reconizing figure handle

11 次查看(过去 30 天)
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',...
'XLim',[0:300],'YLim',[0:100]);
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?
  3 个评论
Gabriel Stanley
Gabriel Stanley 2022-10-7
I know, I went with those because they were easy names in this context. Normally I prepend a descriptive to the object type for graphics handles, e.g.: DamageGrade_Surf, DG_Probability_Surf, etc.
dpb
dpb 2022-10-7
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.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2022-10-7
hF=figure;
hAx=axes(hF,'Position',[0.1300 0.3300 0.7750 0.700]);
xlabel(hAx,'SomeText')
ylabel(hAx,'MoreText')
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.
  2 个评论
Gabriel Stanley
Gabriel Stanley 2022-10-7
Aaaand while I was writing my response to the cyclist, you came in with a direct answer, and an explanation of the functional relationship of the various objects involved. Thank you.
dpb
dpb 2022-10-8
Can be a little more succinct in setting multiple properties on a given object (or collection of objects); the above could be written as
hF=figure;
hAx=axes(hF,'Position',[0.1300 0.3300 0.7750 0.700]);
axis(hAx,[0 300 0 100]) % axis() sets limits, style, ydirection, ...
set([hAx.YLabel;hAx.XLabel],{'String'},{'SomeText';'MoreText'})
NOTA BENE: the vector of object handles to the axis labels and then the 'String' property of those.

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2022-10-7
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.
  2 个评论
Jan
Jan 2022-10-7
编辑:Jan 2022-10-7
Exactly.
axes('XLabel', 'SomeText')
Error using axes
Value must be a handle.
The XLabel must be a handle of a text object. The figure handle was not the problem.
Gabriel Stanley
Gabriel Stanley 2022-10-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by