Matlab GUI Tutorial - Close GUI Confirmation
23 Nov 2007 Quan Quach 13 comments 6,399 views
Introduction
This tutorial will show you how to create a confirmation window when you attempt to close the GUI. Sometimes, the user might accidentally close the GUI window without meaning to. To protect against this, we can implement a confirmation window that appears when the user attempts to close the GUI. This feature can protect the user from losing any work that has been performed.

This tutorial is written for those with some experience creating a Matlab GUI. If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is highly recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!
Creating the Confirmation Window: Method 1
- First, you need to add the following line of code to the opening function of your GUI.
set(handles.figure1,'CloseRequestFcn',@closeGUI);
This line is of code is telling matlab to run the closeGUI function once someone attempts to close the GUI figure. Make sure to put this line of code before the line
guidata(hObject, handles);
-
Next, copy this code at the end of the GUI .m file OR in a separate .m file, but make sure it’s saved in the same directory where the GUI files are located.
function closeGUI(src,evnt) %src is the handle of the object generating the callback (the source of the event) %evnt is the The event data structure (can be empty for some callbacks) selection = questdlg('Do you want to close the GUI?',... 'Close Request Function',... 'Yes','No','Yes'); switch selection, case 'Yes', delete(gcf) case 'No' return end
Creating the Confirmation Window: Method 2
- First, you need to add the following line of code to the opening function of your GUI.
set(handles.figure1,'CloseRequestFcn','closeGUI');
Notice that instead of the @, we use the single quotation marks. By using the single quotation marks, you are assigning a separate m-file to the close request function of the figure. Make sure to put this line of code before the line
guidata(hObject, handles);
-
Next, copy this code into a separate .m file, but make sure it’s saved in the same directory where the GUI files are located. Notice that we did not define any input arguments here.
function closeGUI selection = questdlg('Do you want to close the GUI?',... 'Close Request Function',... 'Yes','No','Yes'); switch selection, case 'Yes', delete(gcf) case 'No' return end
And that’s it!!! Pretty simple like I mentioned. If you want to see an example, go ahead to the next page.
Pages: 1 2
13 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.
Hello Anonymous,
That is a good question
Here’s what you can do:
Ah thank you very much. Worked perfectly. After reading the Matlab documentation on GUIDATA, it said that you could also do guidata(gcbo). What’s the difference? GCBO refers to the current GUI right? src I’m guessing does the same thing, with the source in this case being the current GUI as well?
When i want to disable all my connections, it came out this error and i could not do anything to my connections.
“This connection maybe using one or more protocols that do not support Plug-And-Play, or it may have been initiated by other user or the system account.”
Hope u are able to help me. Thanks.
Hi QQ,
Thanks again for the tutorials! I’ve tried the code out, and it works perfectly if there is one gui open, but if there are two present, then closing function always closes the first one (when I’ve written it in the second one!)
I’m assuming that I have to specify the actual handle of the figure that I want it to close, but I’m at a loss at how to do it.
Gratitude to wisdom!
thank you very much for the script
Hello !
I need to source code matlab GUI ‘Thermodynamic close system’ condition are design to be spring
thank you
Fantastic tutorials hats off to you, tutorials like these can certainly motivate beginners and inspire them to go more deeper into the programming. Thanks a lot
Hi
I found this tutorial very interesting but i have small problem
My gui has a close button which will stop the current operation and then close the GUI.
now when i use
set(handles.figure1,’CloseRequestFcn’,@closeGUI);
this line and its function my code enters infinite loop.
here is my full code
hy…
can anyone help me out..m new in matlab
i m writng a code for motion detention,i have three buttons in ma gui one is start and other is stop,close….
by presing stop m unable to stop my curntly ruinng video,and by pressing close i am unable to close my GUi…..
wot to dooo
i have tried…
closeall;
delet(fig);
bt nt wrkng….