Matlab GUI Tutorial - Close GUI Confirmation
23 Nov 2007 Quan Quach 4 comments 6094 views
Example of the Confirmation Window in Effect
-
First, download the sample GUI here. Unzip the files and place them wherever you please.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
Add the following code to the opening function, closeGUI_tutorial_OpeningFcn
set(handles.figure1,'CloseRequestFcn',@closeGUI);
Make sure to add the above line of code before this line of code:
guidata(hObject, handles);
-
Now, add the following code to the end of the GUI m-file.
function closeGUI(src,evnt) %this function is called when the user attempts to close the GUI window %this command brings up the close window dialog selection = questdlg('Do you want to close the GUI?',... 'Close Request Function',... 'Yes','No','Yes'); %if user clicks yes, the GUI window is closed, otherwise %nothing happens switch selection, case 'Yes', delete(gcf) case 'No' return end
-
Now, save your .m file and run the GUI. You should see the following GUI appear

-
Now, try to close the GUI. You should see the following appear:

-
Click “yes” to close the GUI, or click “no” to keep it open.
This is the end of the tutorial.
Pages: 1 2
4 Responses to “Matlab GUI Tutorial - Close GUI Confirmation”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Hello!
I have found some really interesting tricks and guidelines for GUI programming in Matlab. I was testing this particular code, and Matlab will output an error:
??? Undefined function or variable ‘closeGUI’.
??? Error while evaluating figure CloseRequestFcn
I corrected it by changing the opening function line to
set(handles.figure1,’CloseRequestFcn’,@closeGUI);
and also, by changing the closeGUI function line to accept 2 arguments (src,evnt) as per matlab’s help
function my_closereq(src,evnt)
% User-defined close request function
% to display a question dialog box
selection = questdlg(’Close This Figure?’,…
‘Close Request Function’,…
‘Yes’,'No’,'Yes’);
switch selection,
case ‘Yes’,
delete(gcf)
case ‘No’
return
end
thanks oscar for pointing that out. there are two ways to go about doing this.
you can define the closeGUI function from within the GUI .m file, or you can define the closeGUI function inside a separate m-file. i will update the tutorial to show how to do it both ways.
Heya,
could you wright a small piece of
% your details
if we would like to give you credits in our code for your functions
Thanks for the help
Hey Quan, all of your tutorials are very helpful. I have a quick question though, how do you pass the handles structure into the the closeGUI function? I wanted to clean up some serial port objects before my GUI closes, but I can’t seem to get it to recognize the handles structure.
This is an idea of what I want to do:
function closeGUI(src,evnt,handles)
fclose(handles.port);
delete(handles.port);
clear handles.port;
Thanks in advance.