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

8 次查看(过去 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:

更多回答(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