MATLAB Answers

How to check if handle is to a deleted axes?

조회 수: 183(최근 30일)
K E 10 Nov 2015
댓글: Mike Garrity 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
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 10 Nov 2015
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 10 Nov 2015
Confirming that both work in my example. Thanks!
Adam 10 Nov 2015
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 11 Nov 2015
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 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)
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 10 Nov 2015
Confirming this works on my example. Thanks!
Star Strider
Star Strider 10 Nov 2015
My pleasure!

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by