Axes not reconizing figure handle

3 visualizaciones (últimos 30 días)
Gabriel Stanley
Gabriel Stanley el 7 de Oct. de 2022
Comentada: dpb el 8 de Oct. de 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',...
'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 comentarios
Gabriel Stanley
Gabriel Stanley el 7 de Oct. de 2022
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 el 7 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 7 de Oct. de 2022
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 comentarios
Gabriel Stanley
Gabriel Stanley el 7 de Oct. de 2022
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 el 8 de Oct. de 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
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.

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 7 de Oct. de 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.
  2 comentarios
Jan
Jan el 7 de Oct. de 2022
Editada: Jan el 7 de Oct. de 2022
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 el 7 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by