Main Content

waitfor

Block execution and wait for condition

Description

example

waitfor(obj) blocks statements from executing until the specified object closes (is deleted). When the object no longer exists, waitfor returns, enabling execution to resume. If the object does not exist, waitfor returns immediately.

example

waitfor(obj,propname) specifies a property name of the object and blocks execution until the value of the property changes or the object closes. For example, waitfor(mytable,'Data') pauses execution until the value of 'Data' changes for mytable. If the specified property name is invalid, execution remains blocked.

example

waitfor(obj,propname,propvalue) specifies a value that the property must change to before execution can resume. If the specified property is already equal to propvalue, then waitfor returns immediately and execution resumes.

Examples

collapse all

Create a warning dialog and wait for it to close. Commands after waitfor do not execute until you close the dialog.

mydlg = warndlg('This is a warning.', 'A Warning Dialog');
waitfor(mydlg);
disp('This prints after you close the warning dialog.');

Wait for the user to select a check box before adding data to a table. The Value property of the check box is 0 when not selected, and 1 when selected.

t = uitable;
c = uicontrol('Style','checkbox','String','Add data');
c.Position = [320 100 80 20];
waitfor(c,'Value');
t.Data = magic(5);

Change the background color of a text field when the user stops editing it and clicks elsewhere in the figure. When the text field loses focus, the Editing property changes from 'on' to 'off'.

txt = text(.5,.5,'Edit text and click');
txt.Editing = 'on';
txt.BackgroundColor = [1 1 1];

waitfor(txt,'Editing','off');
txt.BackgroundColor = [1 1 0];

Input Arguments

collapse all

Object, such as an Axes, Text, Panel, ButtonGroup, Table, or UIControl object. The object can be the child of a Figure object created with the figure or uifigure function, or it can be the child of a container in a Figure object.

Property name, specified as a character vector or string scalar. Use this argument to specify a property of obj whose value must change before execution resumes.

Property value, specified as a valid property value associated with propname. Use this argument to indicate a specific value that the property must change to before execution resumes.

Tips

  • If you close the figure while waitfor is executing, an error occurs because the code attempts to access objects that no longer exist. You can handle the error by enclosing waitfor in a try/catch block.

Algorithms

Typically, callbacks can still run if waitfor has been used to prevent programs or Simulink® models from continuing execution. For example, callbacks that respond to user actions (like pressing a mouse button) can still run even if waitfor has been called.

waitfor can also be used to block nested function calls. For example, a callback that executes while the waitfor function is running can also call waitfor.

If a callback function of a UI component is currently executing the waitfor function, then that callback can be interrupted regardless of what the Interruptible property value for that component has been set to.

Version History

Introduced before R2006a