Terminate Matlab function by closing GUI window

12 visualizaciones (últimos 30 días)
mat
mat el 15 de Feb. de 2016
Respondida: Geoff Hayes el 16 de Feb. de 2016
How can I stop running a main function by closing a loaded GUI? I want to make us of the close request function in GUI. With this code I can't close the window.
My code is like this:
Main program
global run terminate;
run=1;terminate=0;
mainGUI %starting GUI
while run
%Program
end
terminate=1;
GUI
function figure1_CloseRequestFcn(hObject, eventdata, handles)
global run terminate
run=0;
while ~terminate
end
delete(hObject);

Respuestas (1)

Geoff Hayes
Geoff Hayes el 16 de Feb. de 2016
Mat - perhaps you could consider implementing a listener to respond to a changed property value. In this case, the listener could listen for a change to a property within your GUI. As it closes, the listener could then set a parameter that would allow you to exit from your main program. For example, suppose we have added a DeleteFcn to our GUI (using GUIDE, right-click on the figure and select View Callbacks->DeleteFcn which should create the function within your GUI m file as
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
with no body. The interesting point is in the comment: executes during object deletion, before destroying properties. We will then set one of the properties before the GUI is destroyed. In this case, we will set the Visible property to off (which hides the GUI) as
set(hObject,'Visible','off');
Now, we will set up a listener for this property. In your main function (if you are just using a script then you will need to modify it so that it function) we do
function guiCloseTest
close all;
hGui = MainGui; % get the handle to the GUI
hListener = addlistener(hGui,'Visible', 'PostSet',@guiVisibleCallback);
doLoop = true;
k = 1;
while doLoop
fprintf('At iteration: %d\n',k);
k = k + 1;
% pause to allow loop to be interrupted
pause(0.001);
end
function guiVisibleCallback(hObject, eventdata)
fprintf('Gui visible property has changed!\n');
doLoop = false;
end
end
We launch the GUI and then add a listener for a change to its Visible property. Note that we make the assumption that this property is only changed when we close the GUI. We then begin our processing in the while loop and at the end of each iteration, pause for one millisecond to allow the loop to be interrupted by some other action. When the user closes the GUI (by pressing the x in the top corner or by some other means), then our DeleteFcn sets the Visible property to off, and our listener callback, guiVisibleCallback fires and sets the doLoop variable to false so that the while loop exits. (In the above code, the callback is a nested function to the main program so that it has access to any local variables (like doLoop) declared in the "parent" function.)
The above example eliminates the need for global variables. Try implementing the above and see what happens!

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by