MATLAB Answers

How to check if handle is to a deleted axes?

227 views (last 30 days)
K E on 10 Nov 2015
Commented: Walter Roberson on 20 Apr 2021 at 19:56
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')

Accepted Answer

Mike Garrity
Mike Garrity on 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.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 20 Apr 2021 at 16:34
Edited: Adam Danz on 20 Apr 2021 at 16:42
How to check if handle is to a deleted object?
Anonymous function to detect if a variable is a deleted object.
isDeletedObj = @(h)isobject(h) & ~isgraphics(h);
Note: Tests below show that this function will also identify graphics placeholders. To eliminate detection of graphics place holders,
isDeletedObj_2 = @(h)isobject(h) & ~isgraphics(h) & ~isa(h,'');
To test for a specific type of deleted object, add a condition that tests the class of the input variable.
isDeletedAxes = @(h)isobject(h) & ~isgraphics(h) & isa(ax,'');
fig = figure();
ax = axes(fig);
h = plot(ax,rand(1,5));
idx = isDeletedObj(ax) % example 1
idx = logical
idx = isDeletedObj([fig, ax, h]) % example 2
idx = 1×3 logical array
0 1 1
Comparison with other methods
Both versions of isDeletedObj() return true for deleted object handles; other functions return false.
tf = [ishandle(ax), ...
ishghandle(ax), ...
isvalid(ax), ...
isgraphics(ax), ...
isDeletedObj(ax), ...
tf = 1×6 logical array
0 0 0 0 1 1
All functions correctly return false for (most) non-graphics objects. Non-graphics inputs can occur when validating inputs to functions that contain optional parent handles as inputs. Since functions other than isDeletedObj and isDeletedObj_2 return false in this example and the example above, a false output does not mean that the input is a deleted object!
% isvalid() removed; will cause error
tf = [ishandle(pi), ...
ishghandle(pi), ...
isgraphics(pi), ...
isDeletedObj(pi), ...
tf = 1×5 logical array
0 0 0 0 0
Both versions of isDeletedObj() correctly return false for existing object handles; other functions correctly return true.
tf = [ishandle(fig), ...
ishghandle(fig), ...
isvalid(fig), ...
isgraphics(fig), ...
isDeletedObj(fig), ...
tf = 1×6 logical array
1 1 1 1 0 0
Both versions of isDeletedObj() correctly return false for an integer that coincides with an existing figure number or matlab root number (0). All other tests fail to distinguish figure handles from their integer representation! This is because the Matlab functions support backward compatibility prior to the graphics changes in r2014b.
% isvalid() removed, will cause error
tf = [ishandle(1), ...
ishghandle(1), ...
isgraphics(1), ...
isDeletedObj(1), ...
tf = 1×5 logical array
1 1 1 0 0
When testing graphics placeholders (gobjects), isDeletedObj() and isvalid() return true while isDeletedObj_2() returns false. This examples shows the only difference between the two versions isDeletedObj.
obj = gobjects(1);
tf = [ishandle(obj), ...
ishghandle(obj), ...
isvalid(obj), ...
isgraphics(obj), ...
isDeletedObj(obj), ...
tf = 1×6 logical array
0 0 1 0 1 0
  • A false output to Matlab's object validity tests (ishandle, ishghandle, isvalid, isgraphics) does not mean that the input is a deleted object.
  • Matlab's object validity tests return true for an input 0 or any integer that corresponds to a an existing figure number.
  • isDeletedObj only returns true when the input is a deleted object or object place holder (gobjects).
  • isDeletedObj_2 only returns true when the input is a deleted object and returns false for obj place holders.

Community Treasure Hunt

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

Start Hunting!