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

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.

 Accepted Answer

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 Comments

Thank you Stephen. The ishandle method works fine!
For robustness reasons you should use ishghandle rather than ishandle.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Tags

Asked:

on 11 Mar 2015

Commented:

on 11 Mar 2015

Community Treasure Hunt

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

Start Hunting!