Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Modal Dialog Box in GUIDE

This example shows how to create a program that opens a modal dialog box when the user clicks a button. The dialog box contains two buttons, and the user must choose one of them. The program responds according to the user's selection in the dialog box.

Create the 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. Right-click the text, "Do you want to create a question dialog?"

    Then, select Property Inspector from the context menu.

  5. In the Property Inspector, select the String property. Then, change the existing value to: Are you sure you want to close?

    Then press Enter.

  6. Select File > Save As.

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

Create the Program That Opens the Dialog Box

Create a separate UI containing a Close button:

  1. While still in GUIDE, 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. In the Property Inspector, select the String property. Then, change the existing value to Close. Then press Enter.

  6. From the File menu, select Save.

  7. In the Save 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 pushbutton1_Callback.

    Then, locate the following generated code in the Editor:

    % --- Executes on button press in pushbutton1.
    function pushbutton1_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)
  8. Add the following code immediately after the comment that begins with % handles....

    % 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
    	delete(handles.figure1)
    end

    When the user clicks the Close button in the closedlg window, the pushbutton1_Callback function executes this command:

    user_response = modaldlg('Title','Confirm Close');
    Recall that the modaldlg function is coded in the other program file, modaldlg.m. That function displays a second window: the Confirm Close dialog box. The return argument, user_response, is the user's selection from that dialog box.

    The switch command decides whether to close the first window (modaldlg) based on the user's selection.

  9. Save your code by pressing Save in the Editor Toolstrip.

Run the Program

  1. In the Command Window, execute the command, closedlg.

  2. MATLAB® displays the closedlg window. Click the Close push button to execute pushbutton1_Callback (in closedlg.m). That function calls modaldlg to display the Confirm Close dialog box.

  3. Click one of the buttons in the Confirm Close dialog box. When you click one of the buttons, modaldlg.m closes the Confirm Close dialog box and returns your selection to the calling function (pushbutton1_Callback). Then, the switch command in that function decides whether to close the remaining open window.

Related Examples

Was this topic helpful?