How to stop the program while closing the figure that shows an animated plot

조회 수: 9 (최근 30일)
Hello everyone,
I've designed a figure which shows a 2-D animation of a plot using drawnow . When I click on close button, it closes the figure but the program still runs for a short while and then gives an error. How can I have the program "understand" that it should stop running as soon as I click on the close button? Thank you in advance.

채택된 답변

Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2015년 3월 11일
Presumably your program uses a loop of some kind. One easy way is to check at the start of the loop for the existence of the figure itself, and break out of the loop if the figure does not exist any more. You can use ishghandle to check specifically for graphics handles:
figH = figure;
for k = 1:frames
if ~ishghandle(figH)
break
end
...
end
Note that this may still result in some errors if the figure is closed at some time during the loop but the code still tries to access the graphics.
Another way is to rely on callbacks. This means integrating the code quite closely with the figure/axes, so that the loop itself is part of a callback. This will complete the current callback before closing the window, so errors do not occur. For an example of this, see my FEX submssion cubehelix_view, and try the demo button:
  댓글 수: 2
Mehdi
Mehdi 2015년 3월 11일
Thank you Stephen. The ishandle method works fine!
Stephen23
Stephen23 2015년 3월 11일
For robustness reasons you should use ishghandle rather than ishandle.

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

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