Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

waitfor - Wait for condition before resuming execution

Syntax

waitfor(h)
waitfor(h,'PropertyName')
waitfor(h,'PropertyName',PropertyValue)

Description

waitfor(h) blocks the caller execution stream until the graphics object identified by handle h is deleted or you type Ctrl+C in the Command Window. h must be scalar. When either of those events occur, waitfor stops blocking execution and returns. If h does not exist, waitfor returns immediately without processing any events.

waitfor(h,'PropertyName'), in addition to the conditions in the previous syntax, stops blocking and returns when the value of 'PropertyName' (any property of the graphics object h) changes. If 'PropertyName' is not a valid property for the object, waitfor returns immediately without processing any events.

waitfor(h,'PropertyName',PropertyValue) stops blocking and returns when the value of 'PropertyName' for the graphics object h changes to PropertyValue. If you previously set 'PropertyName' to PropertyValue, waitfor returns immediately without processing any events.

Definitions

waitfor blocks the caller execution stream so that command-line expressions and statements in the blocked file do not execute until a specified condition occurs. While waitfor blocks an execution stream, other execution streams generated by callbacks that respond to various events (for example, pressing a mouse button) can run, unaffected by waitfor. It also blocks Simulink models from executing. However, callbacks do execute during the blocking of the execution stream. waitfor can block nested execution streams. For example, a callback invoked during a waitfor statement can invoke waitfor.

Examples

Create a plot and pause execution of the rest of the statements until you delete the figure window:

h = figure;
plot(rand(10,1));
disp('Waiting for you to delete the figure...')
drawnow     % Necessary to plot and put message on the screen
waitfor(h)
% The next line only executes when the figure is deleted
disp('Thank you.')
 

Display the current date and time only while a button is depressed

figure('Position',[560 526 420 315]);
hb = uicontrol('Style','togglebutton','Value',0,...
               'Units','normalized',...
               'Position',[.4 .6 .2 .05],...
               'String','Start/Stop');
ht = uicontrol('Style','text','Units','normalized',...
               'Position',[.275 .5 .425 .04],...
               'FontSize',10,...
               'String',datestr(now));
% Iterate 100,000 times then quit
% Typing Ctrl+C in Command Window will also stop the count
count = 0;
while count < 100000        % Exit condition
    waitfor(hb,'Value',1)   %Until togglebutton is down
    % Text only updates while Start/Stop button is down
    set(ht,'String',datestr(now))  % Update date and time
    drawnow                 % Update text field
    count = count+1;
end

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

...
while count < 100000        % Exit condition
     try  % An error occurs if you delete the figure here
        waitfor(hb,'Value',1)   %Until togglebutton is down
        % Text only updates while Start/Stop button is down
        set(ht,'String',datestr(now))  % Update date and time
        drawnow                 % Update text field
    catch ME  % Catch the error and exit gracefully
		   % You can place more code to respond to the error here
        return
    end
end

The ME variable is a MATLAB Exception object that you can use to determine the type of error that occurred. For more information, see Responding to an Exception.

See Also

drawnow | keyboard | pause | uiresume | uiwait | waitforbuttonpress

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS