How to check if handle is to a deleted axes?

조회 수: 108 (최근 30일)
K E
K E 2015년 11월 10일
편집: Adam Danz 2024년 2월 18일
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

채택된 답변

Mike Garrity
Mike Garrity 2015년 11월 10일
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
Captain Karnage
Captain Karnage 2023년 4월 5일
I'm using R2022b
The attached text file contains the output of which -all isvalid
Walter Roberson
Walter Roberson 2023년 4월 5일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2021년 4월 20일
편집: Adam Danz 2024년 2월 18일
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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by