MATLAB: How to build a modal dialog box (GUI) that waits for input from a user

dialoginputMATLABmsgboxuiuitoolsuiwaituser

I want to build a Graphical User Interface (GUI) that waits for input from a user. I would like to know how to create a modal figure that pauses the execution of a MATLAB file to prompt a user for input.

Best Answer

There are several ways to program GUIs to wait for user input.
Method 1: Use one of MATLAB's custom dialog box functions
Here is a list of the dialog box functions shipped with MATLAB that can be used along with UIWAIT, as of MATLAB 7.3 (R2006b):
dialog - Create dialog figure.
errordlg - Error dialog box.
export2wsdlg - Export variables to workspace
helpdlg - Help dialog box.
imageview - Show image in figure with zoom.
inputdlg - Input dialog box.
listdlg - List selection dialog box.
menu - Generate menu of choices for user input.
movieview - Show movie in figure with replay button.
msgbox - Message box.
pagesetupdlg - Page setup dialog.
printdlg - Print dialog box.
printpreview - Display preview of figure to be printed.
questdlg - Question dialog box.
uigetpref - Question dialog box with preference support.
soundview - Show sound in figure and play.
uigetdir - Open standard dialog for selecting a directory
uigetfile - Standard open file dialog box.
uiputfile - Standard save file dialog box.
uisetcolor - Color selection dialog box.
uisetfont - Font selection dialog box.
uiopen - Show open file dialog and call OPEN on result.
uisave - Show open file dialog and call SAVE on result.
uiload - Show open file dialog and call LOAD on result.
uiimport - Start the GUI for importing data (Import Wizard).
waitbar - Display wait bar.
warndlg - Warning dialog box.
For example:
h=helpdlg('Push OK to plot peaks');
uiwait(h);
peaks;
Method 2: UIWAIT
This method involves writing a function which creates the GUI manually. Here is a quick example.
The following function creates a new figure with an editable text box and a push button. The function creates these objects and then makes a call to the UIWAIT function. UIWAIT tells the function to pause its execution until a certain condition is met. In this case, the input argument f (the handle to the GUI figure) tells UIWAIT to pause execution until either "f" is closed or until "uiresume(f)" is called. Pressing the "Done" pushbutton accomplishes two tasks:
- Causes the string property of the editable text box to be updated internally.
- Its CallBack "uiresume(f)" continues the execution of the function.
%%Beginning of function GETINFO %%
function out = getinfo;
f = figure('Units','Normalized',...
'Position',[.4 .4 .3 .3],...
'NumberTitle','off',...
'Name','Info');
e = uicontrol('Style','Edit',...
'Units','Normalized',...
'Position',[.1 .4 .3 .1],...
'Tag','myedit');
p = uicontrol('Style','PushButton',...
'Units','Normalized',...
'Position',[.6 .4 .3 .1],...
'String','Done',...
'CallBack','uiresume(gcbf)');
uiwait(f)
out = str2num(get(e,'String'));
close(f)
To call GETINFO from a MATLAB file or the Command Line:
a = getinfo
This example can be easily extended to include more edit boxes.