Wait for click or key press
In App Designer and apps created using the
uiwait and specifying a
callback that calls
uiresume is recommended as opposed to using
waitforbuttonpress because it provides more control over the app
behavior. For more information, see Alternative Functionality.
w = waitforbuttonpress blocks statements from executing until
the user has clicked a mouse button or pressed a key in the current figure.
The return argument,
w, can have the following values:
0 if it detects a click
1 if it detects a key press
waitforbuttonpress function does not return a value when any of
the following keys are pressed by themselves or together: Ctrl,
Shift, Alt, Caps Lock, Num
Lock, or Scroll Lock.
Create a figure and call the
function. Then, create axes and note that they do not appear.
figure; w = waitforbuttonpress; axes;
Click the figure. Now,
waitforbuttonpress returns, execution
continues, and the axes appear.
Some important points to consider when using
WindowButtonDownFcn is defined for the figure, it executes
waitforbuttonpress returns a value.
waitforbuttonpress function errors if the user closes the
figure by clicking the window close button unless your code calls the
waitforbuttonpress function within a
In App Designer and in apps created using the
uifigure function, use
uiwait to block statements from executing. To resume program execution
when the app user clicks a mouse button or presses a key, specify a
For example, this code creates a UI figure that resumes program execution when a user clicks in the figure window.
fig = uifigure('WindowButtonDownFcn',@(src,event)uiresume(src));
uiwait to block program execution until
uiresume is called or the figure is deleted. Create a
UIAxes object and parent it to the figure. The set of axes does not
uiwait(fig); ax = uiaxes(fig);
Then, click the figure. The program execution resumes and the
Behavior changed in R2014b
To resume program execution after the
is called, a click or key press must occur in the current figure, and not just any figure.
This change in behavior was introduced in order to prevent program execution from proceeding
in an unintended figure. Prior to R2014b,
detect a click or key press event in any open figure. The execution stream would then resume
in the figure in which the event was detected.