MATLAB Answers

K E
0

How to check if handle is to a deleted axes?

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
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 件のコメント

サインイン to comment.

タグ

2 件の回答

Mike Garrity
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 件のコメント

K E
on 10 Nov 2015
Confirming that both work in my example. Thanks!
Adam
on 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.
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.

サインイン to comment.


Star Strider
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 件のコメント

K E
on 10 Nov 2015
Confirming this works on my example. Thanks!
My pleasure!

サインイン to comment.



Translated by