In my GUI I have a simulation that runs with a while loop. Problem is when I close the window while it's still running, matlab gives me an error message of the sort:
Error using matlab.ui.control.UIControl/get
Invalid or deleted object.
Error in SPCgui>startrestart_Callback (line 376)
if get(handles.pauseresume,'Value')==1
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in SPCgui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)SPCgui('startrestart_Callback',hObject,eventdata,gu idata(hObject))
Error while evaluating DestroyedObject Callback
Is there a way to force a loop to stop when I close the window so that this error doesn't occur? I tried using figure1_DeleteFcn and figure1_CloseRequestFcn to no avail.

 Accepted Answer

Image Analyst
Image Analyst on 2 Nov 2016
Maybe try hitting the Pause button (two vertical bars) on the tool ribbon and then click the Quit Debugging button (red square).

5 Comments

I would like to create a standalone window program however, so I need the user to be able to exit the GUI without it giving errors.
I do that all the time and don't have any problem. Show us your OutputFcn() function and the code for your Exit/quit/close button that the user clicks to close the app.
I don't have a code, that's why I'm asking. I'm not sure how to approach this.
So I did actually find a way which uses a global variable.. so my code looks like:
function startrestart_Callback(hObject,eventdata,handles)
global EXIT;
EXIT = 0;
while %condition
%loop code
drawnow
if EXIT==1
return
end
end
function figure1_CloseRequestFcn(hObject,eventdata,handles)
global EXIT;
EXIT = 1;
delete(hObject)
This code will break out of the while loop and delete the GUI figure without giving an error. Yay I guess? I'm sure there are more elegant ways of doing this.
Well I think that loop was sort of running like its own process and when you deleted the handle, the loop kept running which caused the problems. I do something similar - I have a checkbox that I make visible when a lengthy loop starts that the user may wish to interrupt. The checkbox says "Finish Now" and the user checks it if they want to abort the loop. At the bottom of the loop I have
if handles.chkFinishNow.Value
break; % Bail out of loop.
end
After the loop exits, I set the value to 0/off and make the checkbox invisible.
An alternate with with a push button is attached as a small demo.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!