MATLAB Answers

How to check if handle is to a deleted axes?

208 ビュー (過去 30 日間)
K E 2015 年 11 月 10 日
コメント済み: Mike Garrity 2015 年 11 月 11 日
I am running an old plotting routine in R2015b for the first time. It is crashing on a legend command because apparently the axes handle points to a deleted axes, and I am stumped on how to check for this. Can you please replace the line (if legh == 'handle to deleted Axes') to correctly check whether the axis is deleted?
figure; % Make demo figure
hAx = gca; % Get demo axis
delete(hAx); % Delete axis, changing state of axis handle
% Replace following line so it correctly checks if axis is deleted
if hAx == 'handle to deleted Axes'
isDeleted = true;
isDeleted = false;
if ~isDeleted
disp('Axis exists. OK to add legend')

  0 件のコメント



Mike Garrity
Mike Garrity 2015 年 11 月 10 日
A couple of choices.
h = plot(1:10);
Either of those will return false for a deleted handle. They differ on what they return true for. The first will return true for any valid handle. The second one will return true only for graphics objects. It also supports an argument to ask if it is a particular type of graphics object.

  3 件のコメント

K E 2015 年 11 月 10 日
Confirming that both work in my example. Thanks!
Adam 2015 年 11 月 10 日
I usually use
ishghandle( h );
There seem to be a plethora of similar functions in this area!
I wasn't aware of
isgraphics( h )
though I do like the specific version:
isgraphics( h, type )
that would shorten some of the multi-line validation I do with ishghandle.
Mike Garrity
Mike Garrity 2015 年 11 月 11 日
It's relatively new. I think it was added in R2014b. If you need your code to run in older versions, you might still need your multi-line validation.


その他の回答 (1 件)

Star Strider
Star Strider 2015 年 11 月 10 日
I would use the ishandle function. Using another plot to demonstrate (code not shown here), see if this does what you want:
h = gca
htest1 = ishandle(h)
htest2 = ishandle(h)
h =
Axes with properties:
XLim: [0.0000e+000 1.0000e+000]
YLim: [258.0000e+000 274.0000e+000]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [130.0000e-003 110.0000e-003 775.0000e-003 815.0000e-003]
Units: 'normalized'
Show all properties
htest1 =
htest2 =

  2 件のコメント

K E 2015 年 11 月 10 日
Confirming this works on my example. Thanks!
Star Strider
Star Strider 2015 年 11 月 10 日
My pleasure!



Community Treasure Hunt

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

Start Hunting!

Translated by