Axes not reconizing figure handle
11 views (last 30 days)
Show older comments
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 Comments
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.
Accepted Answer
dpb
on 7 Oct 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 Comments
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
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.
More Answers (1)
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.
See Also
Categories
Find more on 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!