Terminating script after closing GUI window.

5 views (last 30 days)
Mark
Mark on 5 Apr 2015
Answered: Walter Roberson on 15 Jan 2016
Hi,
my problem is that when Im trying to close my GUI window through the close request function, the script is still going and it spits out an error, because the object it's trying to access is already deleted.
Is there a way to terminate the script in the closing function? In the main program you can do return; pretty easily but here I'm unsure.
I'm using delete(hObject) / delete(handles.figure1)
Could you please help me?
Thank you.
:)
  3 Comments
Mark
Mark on 8 Apr 2015
The code in the close request function is just clc; to clear text from command window and delete(handles.figure1); to close my GUI window. I searched online and didn't really come across a code to completely stop running of the script in midst of waiting for input.
It is fine if the code hasn't started or when it's finished, but I'm implementing several 'stops' where the program waits (waitfor function) until the button 'Next' is pressed and when I try to quit the program then, it's apparent from the error that the code just continues but I've already deleted the figure and all objects in it and it's trying to access it now unsuccessfuly so it crashes.
So i'm need of something to stop the code not just delete the objects.. Something like 'return' but in function.
Error using matlab.ui.control.UIControl/set
Invalid or deleted object.
Error in GUIThesis>waitNext (line 634)
set(h.btnNext,'Value',0);
Error in GUIThesis>btnStart_Callback (line 168)
waitNext()
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUIThesis (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUIThesis('btnStart_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating DestroyedObject Callback
>>
Ali
Ali on 14 Jan 2016
I have the same problem. Did you find out how to fix it Mark?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 Jan 2016
No, callbacks have no direct way of instructing running functions to do anything. Running functions need to query whether they have been asked to do something. For example they could ask whether their figure handle is still valid.
If you are using R2014b or later you can use isgraphics() to determine whether something is a valid graphics handle. If you are using an earlier MATLAB then use ishandle()

Categories

Find more on Graphics Object Programming 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!