How to check if handle is to a deleted axes?
141 views (last 30 days)
Show older comments
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
Accepted Answer
Mike Garrity
on 10 Nov 2015
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.
16 Comments
Captain Karnage
on 5 Apr 2023
Walter Roberson
on 5 Apr 2023
isvalid is not a function, it is a method. If you try to call isvalid with no parameter or with a parameter that is not one of the classes that defines isvalid then MATLAB is going to go searching for a useable isvalid. MATLAB knows that there is isvalid in various toolboxes but at that level it does not know the class support inside those toolboxes, so it does not know that the isvalid in those toolboxes is irrelevant to whatever you are doing.
What is class() of what you are passing to isvalid?
More Answers (1)
Adam Danz
on 20 Apr 2021
Edited: Adam Danz
on 18 Feb 2024
How to check if graphics handle is to a deleted object?
Let's look at some options and decide which is best.
Deleted handles will be detected as objects but not as graphics objects so this line below may seem reasonable.
isDeletedObj = @(h)isobject(h) & ~isgraphics(h);
However, as the tests below show, this function will incorrectly identify graphics placeholders and string objects as deleted objects.
To test for a specific type of deleted object, add a condition that tests the class of the object.
isDeletedAxes = @(h)isobject(h) & ~isgraphics(h) & isa(h,'matlab.graphics.axis.Axes');
Another indicator of a deleted graphics object is to convert its handle to double which should return -1.
isDeletedObj_2 = @(h)double(h)==-1 & isobject(h) & ~isgraphics(h);
Example
fig = figure();
ax = axes(fig);
h = plot(ax,rand(1,5));
delete(ax) % delete the axes and line
idx = isDeletedObj_2([fig, ax, h])
Comparison with other methods
Test deleted axes.
tf = [ishandle(ax), ...
ishghandle(ax), ...
isvalid(ax), ...
isgraphics(ax), ...
isDeletedAxes(ax), ... % ✅
isDeletedObj(ax), ... % ✅
isDeletedObj_2(ax)] % ✅
Test existing object handle.
tf = [ishandle(fig), ...
ishghandle(fig), ...
isvalid(fig), ...
isgraphics(fig), ...
isDeletedAxes(fig), ... % ✅
isDeletedObj(fig), ... % ✅
isDeletedObj_2(fig)] % ✅
Test graphics placeholder (gobjects).
obj = gobjects(1);
tf = [ishandle(obj), ...
ishghandle(obj), ...
isvalid(obj), ...
isgraphics(obj), ...
isDeletedAxes(obj), ... % ✅
isDeletedObj(obj), ... % ❌
isDeletedObj_2(obj)] % ✅
Test integers that could be interpreted as double handles from MATLAB graphics prior to r2014b.
% isvalid() removed, will cause error
tf = [ishandle(1), ...
ishghandle(1), ...
isgraphics(1), ...
isDeletedAxes(1), ... % ✅
isDeletedObj(1), ... % ✅
isDeletedObj_2(1)] % ✅
Test a non-handle numeric value.
% isvalid() removed; will cause error
tf = [ishandle(pi), ...
ishghandle(pi), ...
isgraphics(pi), ...
isDeletedAxes(pi), ... % ✅
isDeletedObj(pi), ... % ✅
isDeletedObj_2(pi)] % ✅
Test string objects
% isvalid() removed, will cause error
str = "test";
tf = [ishandle(str), ...
ishghandle(str), ...
isgraphics(str), ...
isDeletedAxes(str), ... % ✅
isDeletedObj(str), ... % ❌
isDeletedObj_2(str)] % ✅
0 Comments
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!