How to check if handle is to a deleted axes?
172 views (last 30 days)
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;
disp('Axis exists. OK to add legend')
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.
More Answers (1)
Adam Danz on 20 Apr 2021
Edited: Adam Danz on 20 Apr 2021
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,'matlab.graphics.GraphicsPlaceholder');
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,'matlab.graphics.axis.Axes');
fig = figure();
ax = axes(fig);
h = plot(ax,rand(1,5));
idx = isDeletedObj(ax) % example 1
idx = isDeletedObj([fig, ax, h]) % example 2
Comparison with other methods
Both versions of isDeletedObj() return true for deleted object handles; other functions return false.
tf = [ishandle(ax), ...
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), ...
Both versions of isDeletedObj() correctly return false for existing object handles; other functions correctly return true.
tf = [ishandle(fig), ...
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), ...
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), ...
- 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.