Modal Dialog Box in GUIDE

About the Example

This example shows how to create a modal dialog box to work with a UI that has a Close button. Clicking the Close button displays the modal dialog box, which asks Are you sure you want to close?.

Set Up the Close Confirmation Dialog Box

  1. On the Home tab, in the Environment section, click Preferences > GUIDE > Show names in component palette.

  2. In the Command window, type guide.

  3. In the GUIDE Quick Start dialog box, select Modal Question Dialog. Then, click OK.

  4. In the Layout Editor, right-click the static text, Do you want to create a question dialog?, and select Property Inspector.

  5. Change the String property value to Are you sure you want to close?

  6. In the Layout Editor select File > Save.

  7. In the Save As dialog box, in the File name field, type modaldlg.fig.

Set Up a UI with a Close Button

To set up a separate UI with a Close button:

  1. In the GUIDE Layout Editor, select File > New.

  2. In the GUIDE Quick Start dialog box, select Blank GUI (Default). Then, click OK.

  3. From the component palette on the left, drag a push button into the layout area.

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

  5. Change the String property value to Close.

  6. Change the Tag property value to close_pushbutton.

  7. From the File menu, select Save.

  8. In Save As dialog box, in the File name field, type closedlg.fig. Then, click Save.

    The code file, closedlg.m, opens in the Editor.

    On the Editor tab, in the Navigate section, click Go To, and then select close_pushbutton_Callback.

    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:

    % Get the current position 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 application window
    	%                  .
    	%                  .
    	%                  .
  10. Save closedlg.m.

Run the Program

  1. On the Layout Editor toolbar, click the Run button .

  2. In the closedlg dialog box, click the Close push button.

    The modal dialog box opens.

  3. Click Yes or No.

    • Yes closes both dialog boxes.

    • No closes just the Confirm Close dialog box.

How Close Confirmation Dialogs Work

This section describes how the dialogs work:

  1. When you click the Close button, the close_pushbutton_Callback performs these tasks:

    1. Gets the current position of the window from the handles structure with the command:

      pos_size = get(handles.figure1,'Position')
    2. Calls the modal dialog box with the command:

      user_response = modaldlg('Title','Confirm Close');

        Tip   This is an example of calling a UI 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 code that calls it.

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

    • Makes the dialog box modal.

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

  4. When you click 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:


  6. When the UI with the Close button regains control, it receives the string Yes or No. If the string is 'No', it does nothing. If the string is 'Yes', the Close button callback closes the UI with the command:

Was this topic helpful?