필터 지우기
필터 지우기

Check if handle to axes exists at all [R2017b]

조회 수: 5 (최근 30일)
TJ
TJ 2018년 1월 17일
댓글: TJ 2018년 1월 18일
Hi,
I am trying to check if an axes handle exists and if not create it, e.g.:
X = [1:100];
Y=sin(X);
if handle exists
a=line(h, X, Y);
else
f=figure();
h=axes(figure);
end
In the case where it hasn't been created I keep getting an error 'Undefined function or variable' when I use ishandle or any get statement.

채택된 답변

Jan
Jan 2018년 1월 17일
편집: Jan 2018년 1월 17일
The code is not meaningful. You can always know, if the axes has been created before or not. Therefore it is not clear, what you are exactly asking for. Maybe this helps:
f = gobjects(0);
h = gobjects(0);
% Then a magic section, which does not let you know if the axes is
% existing or not:
if rand > 0.5
f = figure;
h = axes; %
end
...
if ~isgraphics(h) % Better than ishandle
f = figure;
h = axes; %
end
line(x, 1:10, rand(1, 10))
So define the variable holding the handle as an empty graphics object at first. The empty matrix would work also. Then you can check if the variable contains a valid graphics object.
  댓글 수: 5
Jan
Jan 2018년 1월 17일
In both scenarios using the method I've suggested works well. Example:
FigH = figure;
axes;
LineH = gobjects(0);
for k = 1:1000
if isgraphics(h) % of ishghandle(h)
if isgraphics(LineH)
LineH = plot(1:10, rand(1, 10));
else
set(LineH, 'YData', rand(1, 10));
pause(1);
end
else
disp('User has closed the figure');
break;
end
end
Here isgraphics is used to check, if the figure is still existing and if the line object has been created before, to allow a cheaper setting of YData instead of a re-creation.
Does this help?
TJ
TJ 2018년 1월 18일
Hi Jan,
Using this I managed to achieve my goal. Thank you.

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

추가 답변 (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