Terminate Matlab function by closing GUI window

조회 수: 7 (최근 30일)
mat
mat 2016년 2월 15일
답변: Geoff Hayes 2016년 2월 16일
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);

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 2월 16일
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!

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by