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.
On the Home tab, in the Environment section, click Preferences > GUIDE > Show names in component palette.
In the Command Window, type
In the GUIDE Quick Start dialog box, select Modal Question Dialog. Then, click OK.
Right-click the text, "Do you want to create a question dialog?"
Then, select Property Inspector from the context menu.
In the Property Inspector, select the
Then, change the existing value to:
Are you sure you want
Then press Enter.
Select File > Save As.
In the Save As dialog box, in the File name field,
Create a separate UI containing a Close button:
While still in GUIDE, select File > New.
In the GUIDE Quick Start dialog box, select Blank GUI (Default). Then, click OK.
From the component palette on the left, drag a push button into the layout area.
Right-click the push button and select Property Inspector.
In the Property Inspector, select
String property. Then, change the existing
Close. Then press Enter.
From the File menu, select Save.
In the Save dialog box, in the File
name field, type
The code file,
closedlg.m, opens in the Editor.
On the Editor tab, in the Navigate section,
click Go To, and then select
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)
Add the following code immediately
after the comment that begins with
% 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
executes this command:
user_response = modaldlg('Title','Confirm Close');
modaldlgfunction 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.
switch command decides whether to close
the first window (modaldlg) based on the user's selection.
Save your code by pressing Save in the Editor Toolstrip.
In the Command Window, execute the command,
MATLAB® displays the closedlg window. Click the Close push
button to execute
That function calls
modaldlg to display the Confirm
Close dialog box.
Click one of the buttons in the Confirm Close dialog
box. When you click one of the buttons,
the Confirm Close dialog box and returns your selection to the calling
pushbutton1_Callback). Then, the
in that function decides whether to close the remaining open window.