How to check if handle is to a deleted axes?
조회 수: 108 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
2023년 4월 5일
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
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])
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!