How to check if handle is to a deleted axes?

110 Ansichten (letzte 30 Tage)
K E
K E am 10 Nov. 2015
Bearbeitet: Adam Danz am 18 Feb. 2024
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

Akzeptierte Antwort

Mike Garrity
Mike Garrity am 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 Kommentare
Captain Karnage
Captain Karnage am 5 Apr. 2023
I'm using R2022b
The attached text file contains the output of which -all isvalid
Walter Roberson
Walter Roberson am 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 20 Apr. 2021
Bearbeitet: Adam Danz am 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])
idx = 1x3 logical array
0 1 1
Comparison with other methods
Test deleted axes.
tf = [ishandle(ax), ...
ishghandle(ax), ...
isvalid(ax), ...
isgraphics(ax), ...
isDeletedAxes(ax), ... % ✅
isDeletedObj(ax), ... % ✅
isDeletedObj_2(ax)] % ✅
tf = 1x7 logical array
0 0 0 0 1 1 1
Test existing object handle.
tf = [ishandle(fig), ...
ishghandle(fig), ...
isvalid(fig), ...
isgraphics(fig), ...
isDeletedAxes(fig), ... % ✅
isDeletedObj(fig), ... % ✅
isDeletedObj_2(fig)] % ✅
tf = 1x7 logical array
1 1 1 1 0 0 0
Test graphics placeholder (gobjects).
obj = gobjects(1);
tf = [ishandle(obj), ...
ishghandle(obj), ...
isvalid(obj), ...
isgraphics(obj), ...
isDeletedAxes(obj), ... % ✅
isDeletedObj(obj), ... % ❌
isDeletedObj_2(obj)] % ✅
tf = 1x7 logical array
0 0 1 0 0 1 0
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)] % ✅
tf = 1x6 logical array
1 1 1 0 0 0
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)] % ✅
tf = 1x6 logical array
0 0 0 0 0 0
Test string objects
% isvalid() removed, will cause error
str = "test";
tf = [ishandle(str), ...
ishghandle(str), ...
isgraphics(str), ...
isDeletedAxes(str), ... % ✅
isDeletedObj(str), ... % ❌
isDeletedObj_2(str)] % ✅
tf = 1x6 logical array
0 0 0 0 1 0

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by