This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

waitfor

Block execution and wait for condition

Syntax

waitfor(mo)
waitfor(mo,propname)
waitfor(mo,propname,propvalue)

Description

example

waitfor(mo) blocks statements from executing until the MATLAB® 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(mo,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(mo,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

Block execution until a warning dialog is closed.

Create a warning dialog and call the waitfor function with the warning dialog specified as the object. Then, create a message to display at the command line. Note that it does not appear.

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

Close the warning dialog. Now, waitfor returns, execution resumes, and the message is displayed at the command line.

Wait for the user to select a check box before adding data to a table. Detect when the check box is selected by watching its Value property.

Create a table and a check box. Set the Value property of the check box to 0. Call the waitfor function and specify that the Value property of the check box must change before execution continues. Then, specify data to be added to the table. Note that the data doesn't appear.

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

Select the check box. This changes the check box Value property to 1. Execution resumes and the specified data appears in the table.

Wait for the user to stop editing a text field before changing the background color of the text. Detect when the user stops editing and clicks elsewhere in the figure by waiting for the Editing property of the text field to become 'off'.

Create a Text object with some initial text to display. Then, set its Editing property to 'on' and set its background color to white.

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

Next, call waitfor and specify that the value of the Editing property must change to 'off' before execution resumes. Set the text background color to yellow and note that it does not yet change.

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

Click on the text field and edit its contents. Then, click elsewhere in the figure. Now, waitfor returns, execution resumes, and the text background color changes to yellow.

Input Arguments

collapse all

Any MATLAB object, including Graphics objects, such as Axes, Text, Panel, ButtonGroup, Table, or UIControl objects.

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

Property value associated with propname, specified as any MATLAB data. For valid propvalue data types, refer to the MATLAB reference page for the type of object you have specified. Use this argument to indicate a specific value that the property must change to before execution continues.

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.

Introduced before R2006a