MATLAB Answers

K E
0

How to check if handle is to a deleted axes?

Asked by K E
on 10 Nov 2015
Latest activity Commented on by Mike Garrity
on 11 Nov 2015
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
peaks;
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;
else
isDeleted = false;
end
if ~isDeleted
disp('Axis exists. OK to add legend')
end

  0 Comments

Sign in to comment.

2 Answers

Answer by Mike Garrity
on 10 Nov 2015
 Accepted Answer

A couple of choices.
h = plot(1:10);
delete(h)
isvalid(h)
isgraphics(h)
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 Comments

Confirming that both work in my example. Thanks!
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.
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.

Sign in to comment.


Answer by Star Strider
on 10 Nov 2015

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)
delete(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 =
1
htest2 =
0

  2 Comments

Confirming this works on my example. Thanks!

Sign in to comment.