startat

Class: timer

Schedule timer to fire at specified time

Description

example

startat(t,firingTime) schedules timer, t, to fire at specified time, firingTime. A timer fires by executing the callback function, timerFcn. firingTime must be within 25 days of the current time.

  • If t is an array of timer objects and firingTime is a scalar, startat sets all the timers to fire at the specified time.

  • If t is an array of timer objects and firingTime is an array of the same size as t, startat sets each timer to fire at the corresponding time.

example

startat(t,Y,M,D) starts the timer and schedules execution of TimerFcn at the year (Y), month (M), and day (D) specified.

example

startat(t,Y,M,D,H,MI,S) also specifies the hour (H), minute (MI), and second (S) specified.

Input Arguments

t

Object of class timer.

firingTime

Time at which the timer object is to fire, specified as a serial date number, character representation of date format, or a date vector. firingTime can be a single date or an array of dates with the same number of rows as timer objects in t.

  • A serial date number indicates the number of days that have elapsed since 1-Jan-0000 (starting at 1). See datenum for additional information about serial date numbers.

  • To specify character representation of dates, use the following date formats defined by the datestr function: 0, 1, 2, 6, 13, 14, 15, 16, or 23. These numeric identifiers correspond to formats defined by the formatOut property of the datestr function. Dates with two-character years are interpreted to be within the 100 years centered on the current year.

  • Date vectors are specified as an m-by-6 or m-by-3 matrix containing m full or partial date vectors, respectively. A full date vector has six elements indicating year, month, day, hour, minute, and second, in that order. A partial date vector has three elements indicating year, month, and day, in that order.

Y,M,D

Time at which the timer object is to fire, specified as numbers indicating the year (Y), month (M), and day (D). Month values less than 1 are set to 1; other arguments can wrap and have negative values.

Y,M,D,H,MI,S

Time at which the timer object is to fire, specified as numbers indicating the year (Y), month (M), day (D), hour (H), minute (MI), and second (S) specified. Month values less than 1 are set to 1; other arguments can wrap and have negative values.

Examples

expand all

Create a timer that displays messages at start time and firing time.

t = timer('TimerFcn', @(~,~)disp('Fired.'), ...
    'StartFcn', @(~,~)disp('Started.'));

Set the timer to fire 2 seconds from the present time using a serial date. A serial date is specified in days.

two = 2/(60^2*24); % two seconds in serial time
fTime = now + two
startat(t,fTime);
fTime =

   7.3527e+05

Started.
Fired.

Wait for the timer to fire, and then delete the timer.

delete(t)

Create a timer that displays messages at start time and firing time.

t = timer('TimerFcn', @(~,~)disp('Fired.'), ...
    'StartFcn', @(~,~)disp('Started.'));

Schedule the timer to start 2 days from present at 00:00:00

[Y, M, D, H, MI, S] = datevec(now+2);
startat(t,Y,M,D)
Started.

Manually stop and delete the timer.

stop(t)
delete(t)

Algorithms

  • The startat method specifies when the timer object executes the TimerFcn callback, not when the timer starts running. The timer starts running with the call to the startat method.

  • Based on the specified time, startat computes and sets the required StartDelay property of the timer object, t. Additionally, it sets the Running property of the timer object to 'on', and executes the StartFcn callback.

  • startat modifies the timer object’s startDelay property. As such, startat overrides specified values of the timer’s startDelay property.

Introduced before R2006a