Main Content

startat

Schedule timer to fire at specified time

Description

Use this function to add a delay between when a timer starts and when the timer callback function, timerFcn, will begin firing.

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. Note that if t has start callback function, startFcn, this function will fire when startat is called and not at the time specified by firingTime.

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

  • If t is an array of timers 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) that you specify.

example

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

Examples

collapse 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 by using a datetime and adding a duration of 2 seconds.

two = seconds(2); % a two second duration
fTime = datetime + two
startat(t,fTime);
fTime = 

  datetime

   14-Aug-2020 16:30:50

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)

Input Arguments

collapse all

Timer to start, specified as a timer object or array of timer objects.

Example: startat(t,firingTime)

Time at which the timer is to fire, specified as a datetime array, 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 values as timer objects in t.

  • A datetime array stores values that represent points in time, including a date and a time of day. A duration can be added to a datetime by using the + operator. For more information, see Represent Dates and Times in MATLAB.

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

  • To specify character representation of dates, use these 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.

Example: startat(t,firingTime)

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.

Example: startat(t,Y,M,D)

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.

Example: startat(t,Y,M,D,H,MI,S)

Version History

Introduced before R2006a

See Also

| | |