| MATLAB® | ![]() |
| On this page… |
|---|
View Completed Layouts and Their GUI M-Files Setting Up the Close Confirmation Dialog Setting Up the GUI with the Close Button |
This example illustrates how to use the modal dialog GUI together with another GUI that has a Close button. Clicking the Close button displays the modal dialog, which asks users to confirm that they really want to proceed with the close operation.
The following figure illustrates the dialog positioned over the GUI application, awaiting the user's response.

If you are reading this in the MATLAB® Help Browser, you can click the following links to display the GUIDE Layout Editor and the MATLAB Editor with a completed version of this example. This enables you to see the values of all component properties and to understand how the components are assembled to create the GUI. You can also see a complete listing of the code that is discussed in the following sections.
Note The following links execute MATLAB commands and are designed to work within the MATLAB Help browser. If you are reading this online or in PDF, you should go to the corresponding section in the MATLAB Help Browser to use the links. |
To set up the dialog, do the following:
In the GUIDE Quick Start dialog, select the Modal Question Dialog template and click OK.
Right-click the static text, Do you want to create a question dialog?, in the Layout Editor and select Property Inspector from the pop-up menu.
Scroll down to String in the Property Inspector and change the String property to Are you sure you want to close?
Select Save from the File menu and type modaldlg.fig in the File name field.
The GUI should now appear as in the following figure.

To set up the second GUI with a Close button, do the following:
In the GUIDE Quick Start dialog, select Blank GUI (Default) and click OK. This opens the blank GUI in a new Layout Editor window.
Drag a push button from the Component palette of the Layout Editor into the layout area.
Right-click the push button and select Property Inspector from the pop-up menu.
Click the M-file Editor icon
on the toolbar of the Layout Editor.
Click the Show functions icon
on the toolbar of the M-file editor
and select close_pushbutton_Callback from the menu.
The following generated code for the Close button callback should appear in the M-file editor:
% --- Executes on button press in close_pushbutton. function close_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to close_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
After these comments, add the following code:
% Get the current position of the GUI from the handles structure
% to pass to the modal dialog.
pos_size = get(handles.figure1,'Position');
% Call modaldlg with the argument 'Position'.
user_response = modaldlg('Title','Confirm Close');
switch user_response
case {'No'}
% take no action
case 'Yes'
% Prepare to close GUI application window
% .
% .
% .
delete(handles.figure1)
endRun the GUI with the Close button by clicking the Run button on the Layout Editor toolbar. The GUI appears as in the following figure:

When you click the Close button on the GUI, the modal dialog appears as shown in the following figure:

Clicking the Yes button closes both the close dialog and the GUI that calls it. Clicking the No button closes just the dialog.
This section describes what occurs when you click the Close button on the GUI:
User clicks the Close button. Its callback then
Gets the current position of the GUI from the handles structure with the command
pos_size = get(handles.figure1,'Position')
Calls the modal dialog with the command
user_response = modaldlg('Title','Confirm Close');This is an example of calling a GUI with a property value pair. In this case, the figure property is 'Title', and its value is the string 'Confirm Close'. Opening modaldlg with this syntax displays the text "Confirm Close" at the top of the dialog.
The modal dialog opens with the 'Position' obtained from the GUI that calls it.
The opening function in the modal dialog M-file:
Makes the dialog modal.
Executes the uiwait command, which causes the dialog to wait for the user to click the Yes button or the No button, or click the close box (X) on the window border.
When a user clicks one of the two push buttons, the callback for the push button
Updates the output field in the handles structure
Executes uiresume to return control to the opening function where uiwait is called.
The output function is called, which returns the string Yes or No as an output argument, and deletes the dialog with the command
delete(handles.figure1)
When the GUI with the Close button regains control, it receives the string Yes or No. If the answer is 'No', it does nothing. If the answer is 'Yes', the Close button callback closes the GUI with the command
delete(handles.figure1)
![]() | An Address Book Reader | Creating GUIs Programmatically | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |