Closing gui window *.gif

I created gui that has uipanel with two radio buttons. I'm running two endless loops for each radio button that know to switch on from another smoothly. When I close gui window I get errors as follows:
??? Error using ==> guidata at 89
H must be the handle to a figure or figure descendent.
Error in ==> Demo>uipanel1_SelectionChangeFcn at 262
guidata(hObject, handles);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Demo at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Demo('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
Error in ==> hgfeval at 63
feval(fcn{1},varargin{:},fcn{2:end});
Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80
hgfeval(cbk, source, evdata);
??? Error while evaluating uicontrol Callback
Can you suggest what is the proper way to close gui window?
Thanks,

5 comentarios

Jan
Jan el 23 de Jul. de 2012
You cannot run two endless loops in Matlab. One endless loop fills the single Matlab thread already. Therefore please post the relevant code to explain, what you are actually doing.
It seems to be obvious, that you have to stop the loops, before the figure can be closed.
Michael Adelman
Michael Adelman el 24 de Jul. de 2012
I cannot show the actual code but I'm going to show the flow:
uipanel1 (hObject, eventdata...)
button1 = get(handles.radiotbutton1,"Value");
button2 = get(handles.radiotbutton2,"Value");
if button1
.
.
.
.
while button 1
.
.
end
end
if button 2
.
.
while button2
.
.
.
end
end
I understand that I need to stop the loop, when I click on 'X' of the window what object or value of the object I need to check to stop the loop?
Jan
Jan el 24 de Jul. de 2012
What does "while button1" mean? Can the value of "button1" be changed from inside the WHILE loop?
Michael Adelman
Michael Adelman el 24 de Jul. de 2012
yes.
button1 or button2 values taken from 'uibuttongroup' panel and then passed to the loops.
Jan
Jan el 25 de Jul. de 2012
Let me ask again: Do you assume, that the value of button1 or button2 is changed, during Matlab processes the infinite loop? Or in other words, is your function equivalent to:
if button1
while true
...
end
end

Iniciar sesión para comentar.

Respuestas (3)

Jan
Jan el 24 de Jul. de 2012

0 votos

A secure solution would be:
  • Create two flags in the figure's local data:
setappdata(FigureHandle, 'LoopEntered', false);
setappdata(FigureHandle, 'StopLoop', false);
  • Enable a flag, when one of the loops is started:
setappdata(FigureHandle, 'LoopEntered', true)
  • When the endless loop is finished (at least when this is not a contradiction...), disable the flag again:
setappdata(FigureHandle, 'LoopEntered', false)
  • Inside the CloreRequestFcn and DeleteFcn (called when the figure is closed or deleted) set another flag to stop the loops:
if getappdata(FigureHandle, 'LoopEntered')
setappdata(FigureHandle, 'StopLoop');
while getappdata(FigureHandle, 'LoopEntered')
pause(0.1);
end
end
  • Inside the loops, check the stop flag:
while XYZ
if get(FigureHandle, 'StopLoop')
break;
end
...
end
setappdata(FigureHandle, 'LoopEntered', false);
Then you have two dynamic locks: The running loop locks the closing of the figure. Closing the figure blocks the execution of the loop.

2 comentarios

Michael Adelman
Michael Adelman el 24 de Jul. de 2012
Hello Jan,
does it matter if I use guide to create gui (for example, figure local data)?
does the code might change?
thanks,
Jan
Jan el 24 de Jul. de 2012
Even GUIDE figures are 100% full and complete Matlab figures. There is not even one spark of magic added by GUIDE. Therefore it does not matter, if you create the figure directly by a program, or if you let GUIDE create the program to create the figure.

Iniciar sesión para comentar.

Michael Adelman
Michael Adelman el 25 de Jul. de 2012

0 votos

Hello Jan,
I followed your suggestion with small adjustments:
1. the endless loops never stops, only switches one to other. Because of that, I left in the CloseRequestFcn and DeleteFcn used these two lines:
if getappdata(handles.figure1, 'LoopEntered')
setappdata(handles.figure1, 'StopLoop', true);
end
2. Once I was able to stop the endless loop, I used these two lines at the end of each endless loop:
setappdata(handles.figure1, 'LoopEntered', false);
delete(handles.figure1);
to delete the window - I get no errors here.
3. Once I deleted figure1, from unknown reason the pointer returns back to the loop and tries to execute the condition that stops endless loop, attached list of errors:
??? Error using ==> getappdata
Invalid object handle
Error in ==> Demo>uipanel1_SelectionChangeFcn at 266
if getappdata(handles.figure1, 'StopLoop')
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Demo at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Demo('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
Error in ==> hgfeval at 63
feval(fcn{1},varargin{:},fcn{2:end});
Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80
hgfeval(cbk, source, evdata);
??? Error using ==> pause
Error while evaluating uicontrol Callback
??? Error using ==> getappdata
Invalid object handle
Error in ==> Demo>uipanel1_SelectionChangeFcn at 352
if getappdata(handles.figure1, 'StopLoop')
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Demo at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Demo('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
Error in ==> hgfeval at 63
feval(fcn{1},varargin{:},fcn{2:end});
Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80
hgfeval(cbk, source, evdata);
??? Error using ==> drawnow
Error while evaluating uicontrol Callback
??? Error using ==> getappdata
Invalid object handle
Error in ==> Demo>uipanel1_SelectionChangeFcn at 266
if getappdata(handles.figure1, 'StopLoop')
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Demo at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Demo('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
Error in ==> hgfeval at 63
feval(fcn{1},varargin{:},fcn{2:end});
Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80
hgfeval(cbk, source, evdata);
??? Error while evaluating uicontrol Callback
Please advice.

5 comentarios

Walter Roberson
Walter Roberson el 25 de Jul. de 2012
Use ishandle(handles.figure1) to test whether the figure is still valid.
Jan
Jan el 25 de Jul. de 2012
Editada: Jan el 25 de Jul. de 2012
"1. the endless loops never stops, only switches one to other." No, Michael, endless loops do not switch one to the other. They run endless and the other loop cannot be reached. Running an endless loop blocks subsequent events, such when you stop one loop, the pending events are processed and e.g. the next callback is executed.
The idea of the endless loops is simply prone to such problems. It seems like your program tries to perform too many things simultaneously. Using e TIMER would be more promissing. But even then avoiding dead-locks and spinning blocks is not easy. I suggest to choose a simpler design with a reduced number of mutual dependencies.
Even Walter's ishandle test can fail, if for any reasons the figure is deleted after the test, but before the update. A non-spaghetti-structure of the program is definitely better.
Michael Adelman
Michael Adelman el 25 de Jul. de 2012
Walter,
if ishandle(handles.figure1) is valid after deletion, how to force deletion?
Walter Roberson
Walter Roberson el 25 de Jul. de 2012
ishandle() is not valid after deletion.
Michael Adelman
Michael Adelman el 25 de Jul. de 2012
Jan, what you say that I need to update before deleting an object?
It's a good idea to use TIMER, but I don't know to use it properly. Once I'll study this I will send some feedbacks about that.

Iniciar sesión para comentar.

Michael Adelman
Michael Adelman el 28 de Jul. de 2012

0 votos

Thank you for your help.
I finally got this the way I want it. Using flags of 'LoopEntered' and 'StopLoop' get the job done. Now I can continue to the next step of the code.
Michael

Categorías

Más información sobre Migrate GUIDE Apps en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Jul. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by