How do I build a modal dialog box (GUI) that waits for input from a user?

51 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Mar 2011
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.
  2 Comments
Abhishek Pandey
Abhishek Pandey on 6 Jun 2016
Edited: MathWorks Support Team on 27 Apr 2023
Hi Angga,
If you are still facing this issue, I would encourage you to contact MathWorks Technical Support Team.
- Abhishek
Image Analyst
Image Analyst on 14 Jul 2017
Angga, look for a call to uiresume() in your second GUI's code and put a breakpoint there.
Chances are, it's executing that line and then continuing on to your main, first GUI again without waiting for you to finish interacting with your second GUI.
You should not have a uiresume() in your OpeningFcn() function or OutputFcn() function since both of these functions get run upon startup of a GUI. You only want uiresume() in the callback of the button that shuts down your second GUI (like an "Exit", "Quit", "Done", "Finish", "Close", "Quit" or similarly named button), AND/OR in the CloseRequest function.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!