Skip to Main Content Skip to Search
Product Documentation

Use a Modal Dialog Box to Confirm an Operation (GUIDE)

About the Modal Dialog Example

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 box, which asks users to confirm that they really want to proceed with the close operation.

The following figure illustrates the dialog box positioned over the GUI application, awaiting the user's response.

confirm close  modal dialog box

A modal dialog box blocks a user's access to the Command Window or other MATLAB windows until the user closes it or types Ctrl+C (which turns it into a nonmodal dialog box). You can make any GUI modal, but normally GUIs are nonmodal in order to let users change window focus while they work. The figure property WindowStyle determines whether it is modal or not. Typing Ctrl+C changes a 'modal' WindowStyle to the default value, 'normal'.

Modal figures stack on top of all existing figure windows, making them inaccessible as long as the top figure exists and remains modal. However, any new figures created after a modal figure is displayed (for example, plots or other dialog boxes created by a modal GUI) stack on top of it and are accessible; they can be modal as well.

View and Run the Modal Dialog Box GUIs

If you are reading this document in the MATLAB Help browser, you can access the example FIG-file and code file by clicking the following links. If you are reading this on the Web or in PDF form, go to the corresponding section in the MATLAB Help Browser to use the links.

If you intend to modify the layout or code of this GUI example, first save copies of its code files and FIG-files to your current folder (you need write access to your current folder to do this). Follow these steps to copy the example files to your current folder and then to open them:

  1. Click here to copy the files to your current folder.

  2. Enter guide modaldlg; guide confirm_close or Click here to open the GUI FIG-files in GUIDE.

  3. Enter edit modaldlg; edit confirm_close or Click here to open the GUI code files in the Editor..

You can view the properties of any component by double-clicking it in the Layout Editor to open the Property Inspector for it. You can modify either the figure, the code, or both, and then save the GUI in your current folder using File > Save as from GUIDE. This saves both files, allowing you to rename them, if you choose.

To just inspect the GUI in GUIDE and run it, follow these steps instead:

  1. Click here to add the example files to the MATLAB path (only for the current session).

  2. Click here to run the modal GUIs.

  3. Click here to display the GUIs in the GUIDE Layout Editor (read only).

  4. Click here to display the GUI code files in the MATLAB Editor (read only).

Set Up the Close Confirmation Dialog

To set up the dialog, do the following:

  1. in the GUIDE Layout Editor, select New from the File menu.

  2. In the GUIDE Quick Start dialog box, select the Modal Question Dialog template and click OK.

  3. Right-click the static text, Do you want to create a question dialog?, in the Layout Editor and select Property Inspector from the context menu.

  4. Scroll down to String in the Property Inspector and change the String property to Are you sure you want to close?

  5. From the File menu, select Save and type modaldlg.fig in the File name field.

The GUI looks like the following figure.

finished version of the GUI

Set Up the GUI with the Close Button

To set up the GUI with a Close button:

  1. From the File menu in the GUIDE Layout Editor, select New.

  2. In the GUIDE Quick Start dialog box, select Blank GUI (Default) and click OK. This opens the blank GUI in a new Layout Editor window.

  3. Drag a push button from the Component palette of the Layout Editor into the layout area.

  4. Right-click the push button and select Property Inspector from the context menu.

  5. Change the String property to 'Close'.

  6. Change the Tag property to 'close_pushbutton'.

  7. Click the Editor icon on the toolbar of the Layout Editor.

  8. Click the Show functions icon on the toolbar of the editor and select close_pushbutton_Callback from the drop-down menu.

    The following generated code for the Close button callback appears in the 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)
  9. After the preceding 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)
    end

Run the Close-Confirmation GUI

Run the GUI with the Close button by clicking the Run button on the Layout Editor toolbar. The GUI looks like the following figure.

GUI with close button

When you click the Close button on the GUI, the modal dialog box opens, as shown in the following figure.

finished modal dialog for the confirm close GUI

Clicking the Yes button closes both the close dialog and the GUI that calls it. Clicking the No button closes just the dialog.

How the Close-Confirmation GUIs Work

This section describes what occurs when you click the Close button on the GUI:

  1. 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 box 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 box.

  2. The modal dialog box opens with the 'Position' obtained from the GUI that calls it.

  3. The opening function in the modal modaldlg code file:

    • Makes the dialog modal.

    • Executes the uiwait command, which causes the dialog box to wait for the user to click Yes or No, or click the close box (X) on the window border.

  4. 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.

  5. The output function is called, which returns the string Yes or No as an output argument, and deletes the dialog box with the command:

    delete(handles.figure1)

  6. 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)

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS