waitfor - Block execution and wait for condition - MATLAB (original) (raw)
Block execution and wait for condition
Syntax
Description
waitfor([obj](#mw%5Fe1d55a0f-ac45-439f-b2e1-5075607083fb))
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.
waitfor([obj](#mw%5Fe1d55a0f-ac45-439f-b2e1-5075607083fb),[propname](#mw%5F2b169aee-cdde-4c37-98ec-2dd0ec394263))
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 formytable
. If the specified property name is invalid, execution remains blocked.
waitfor([obj](#mw%5Fe1d55a0f-ac45-439f-b2e1-5075607083fb),[propname](#mw%5F2b169aee-cdde-4c37-98ec-2dd0ec394263),[propvalue](#mw%5F187b6595-4f54-4b8a-ab6d-3d83186a620a))
specifies a value that the property must change to before execution can resume. If the specified property is already equal to propvalue
, thenwaitfor
returns immediately and execution resumes.
Examples
Wait for Warning Dialog to Close
Create a warning dialog and wait for it to close. Commands afterwaitfor
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 Property Value Change
Wait for the user to select a check box before adding data to a table. TheValue
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);
Wait for Property to Change to Specific Value
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, theEditing
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
obj
— Object
object
Object, such as an Axes
, Text
,Panel
, ButtonGroup
, Table
, orUIControl
object. The object can be the child of aFigure
object created with the figure or uifigure function, or it can be the child of a container in a Figure
object.
propname
— Property name
character vector | string scalar
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.
propvalue
— Property value
valid property value associated with propname
Property value, specified as a valid property value associated withpropname. 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 enclosingwaitfor
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 thewaitfor
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