GUI still executing code after it is closed

2 views (last 30 days)
I have a Matlab GUI and when a push button is pressed it performs a very long computation(hours long). If during this computation I decide I want to stop or doing something else I will press the close button(default top right corner). This will close the GUI(via an interrupt). However after the figure has closed the code continues to run, eating up system resources, and displaying graphs. When running matlab this hasn't been problem as I just use ctrl C to stop this as well. Now that I have compiled this and am about to redistribute this I need a way to completely stop the code without having the users do control alt delete.
I have tried various things in the CloseRequestFcn such as: 1) delete(handles.gui) 2) set(groot,'ShowHiddenHandles','on') c = get(groot,'Children'); delete(c) 3) delete(hObject); 4) close all force
None of which have worked.

Answers (1)

Walter Roberson
Walter Roberson on 27 Aug 2015
Graphics interrupts are not executed until the code calls figure() or pause() or drawnow() or uiwait() or waitfor()
You could have your code structured something like,
myfig = handle of your figure
user_quit = false;
for K = 1 : 324
for J = 1 : 3439324
drawnow();
if ~isgraphics(myfig);
%user closed the figure. React appropriately
user_quit = true;
break;
end
%now do a bite-sized amount of code, a couple of seconds or so
....
end
if user_quit; break; end
end
I used this structure as an example, to illustrate the point that "break" only breaks out of the innermost loop so you may need a check to break out of the outer loop
  1 Comment
Adam Neufeldt
Adam Neufeldt on 27 Aug 2015
Thanks for the quick response, I actually do not need the drawnow command because from the second link on interrupts I posted:
"Note: Callback interruption and execution behave differently in these situations: If the interrupting callback is a DeleteFcn, CloseRequestFcn, or SizeChangedFcn callback, then the interruption occurs regardless of the Interruptible property value."
In other words when the CloseRequestFcn is called it doesn't require a drawnow, and the interrupt is called(that is why the figure still closes, and I can interrupt any operation I want).
Ideally I would like to avoid having to surround all of my code that involves heavy computation in if statements as there are a number of sections of code like this. But I might end up doing that if that is the only solution.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!