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.

 채택된 답변

Image Analyst
Image Analyst 2016년 11월 2일

0 개 추천

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

댓글 수: 5

Denis Kartachov
Denis Kartachov 2016년 11월 2일
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.
Image Analyst
Image Analyst 2016년 11월 2일
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.
Denis Kartachov
Denis Kartachov 2016년 11월 2일
I don't have a code, that's why I'm asking. I'm not sure how to approach this.
Denis Kartachov
Denis Kartachov 2016년 11월 2일
편집: Denis Kartachov 2016년 11월 2일
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.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 11월 2일

댓글:

2016년 11월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by