Main Content

uiwait

Block program execution and wait to resume

Description

uiwait blocks execution until the uiresume function is called or the current figure (gcf) is deleted.

The uiwait function blocks MATLAB® and Simulink® program execution. uiwait also blocks the execution of Simulink models.

example

uiwait(f) blocks execution until the uiresume function is called or the figure f is deleted. The figure can be one that is created with either the figure or uifigure function.

Use the uiwait function with a modal dialog box to block program execution and restrict user interaction to only the dialog box until the user responds to it.

example

uiwait(f,timeout) blocks execution until uiresume is called, the figure is deleted, or timeout seconds elapse.

Examples

collapse all

Create an alert dialog box and wait for the user to respond to it before allowing program execution to continue.

Create a line plot in a UI figure and display an alert dialog box. Specify a CloseFcn callback for the dialog box that calls the uiresume function when the user responds to it. Wait for the user to click OK in the dialog box or close it. When program execution resumes, display a message in the Command Window.

fig = uifigure;
fig.Position = [500 500 500 350]; 
ax = uiaxes(fig);
plot(ax,1:10)

uialert(fig,'A line plot was created in the axes.', ...
    'Program Information','Icon','info','CloseFcn','uiresume(fig)')

uiwait(fig)
disp('Program execution resumed')

Alert dialog box in a UI figure window with a plot. The dialog box text says: "A line plot was created in the axes."

Block program execution from continuing until the user responds to a modal message dialog box.

Create a line plot in a figure and display a modal message dialog box. Wait for the dialog box to be deleted when the user clicks OK or closes it. When program execution resumes, display a message in the Command Window.

f = figure;
plot(1:10)
msgfig = msgbox('Operation was completed successfully!','Success','modal');
uiwait(msgfig)
disp('Program execution resumed.');

A modal dialog box displays in front of a figure window that contains a line plot.

Create a Continue button and wait until the user presses it. Then display a message.

Create a push button with a callback that calls the uiresume function when it is clicked. Wait for the user to click Continue or close the figure window. Then display a message.

f = figure('Position',[500 500 400 300]);
c = uicontrol('String','Continue','Callback','uiresume(f)');
uiwait(f)
disp('Program execution has resumed');

A "Continue" button displays in the lower-left corner of a figure window.

Create a figure that closes after a specified time.

Create a line plot in a UI figure.

fig = uifigure;
fig.Position = [500 500 500 350];
ax = uiaxes(fig);
plot(ax,1:10);

Create a five-second timeout. Then, close the figure by calling the close function from within a try block. If it has already been closed, the catch block prevents the error, Invalid figure handle, from displaying and allows the code to continue executing normally.

uiwait(fig,5)

try
close(fig)
catch
end

Input Arguments

collapse all

Figure object, specified as a Figure object created with either the figure or uifigure function.

Timeout duration, specified as a numeric value in seconds. Specify a number greater than or equal to 1.

More About

collapse all

Modal Dialog Box

A modal dialog box prevents a user from interacting with other MATLAB windows before responding to the dialog box.

Version History

Introduced before R2006a

See Also

| |