isregular - Determine if input times are regular with respect to time or calendar
unit - MATLAB ([original](http://www.mathworks.com/help/matlab/ref/timetable.isregular.html)) ([raw](?raw))
Determine if input times are regular with respect to time or calendar unit
Syntax
Description
[tf](#bvdhi2s-TF) = isregular([D](#bvdhi2s-TT))
returns 1
(true
) if the input times are_regular_. Otherwise, it returns 0
(false
). The input array D
is regular if its times are in a sequence that is strictly monotone (either increasing or decreasing) with a unique time step.
The input argument D
can be a:
datetime
vector. (since R2021b)duration
vector. (since R2021b)- Timetable. It is regular if its vector of row times is regular.
This syntax is equivalent to isregular(D,'time')
.
[tf](#bvdhi2s-TF) = isregular([D](#bvdhi2s-TT),[timeUnit](#bvdhi2s-unit))
determines if D
is regular with respect to the specified time or calendar unit.
For example, D
might be regular with respect to months, but irregular with respect to exact elapsed time because different months can have different numbers of days. To determine if D
is regular with respect to months, specify timeUnit
as'months'
.
[[tf](#bvdhi2s-TF),[dt](#bvdhi2s-dt)] = isregular(___)
returns dt
, the time step between consecutive times. If D
is regular, thendt
is either a duration
value or acalendarDuration
value. If D
is not regular, then dt
is a NaN
value.
Examples
Create a duration
vector by using the seconds
function.
D = 1×5 duration 1 sec 2 sec 3 sec 4 sec 5 sec
Test D
using the isregular
function. D
is regular because the time interval between consecutive elements is always the same.
Change the last element of D
.
D = 1×5 duration 1 sec 2 sec 3 sec 4 sec 10 sec
D
is no longer regular.
Create a timetable using a monthly datetime
vector. Determine whether it is regular with respect to time, and then with respect to months.
First, create a timetable whose row times are the first five months of the year 2016
, stored as datetime
values. Add the monthly price of a stock as a timetable variable.
StockPrice = [109.0;107.82;113.17;128.01;116]; M = timetable(datetime(2016,1:5,3)',StockPrice)
M=5×1 timetable Time StockPrice ___________ __________
03-Jan-2016 109
03-Feb-2016 107.82
03-Mar-2016 113.17
03-Apr-2016 128.01
03-May-2016 116
Determine if M
is a regular timetable.
M
is not regular with respect to time because the first five months have different numbers of days. Therefore, the exact amount of time between consecutive row times differs from row to row. You can use the diff
function to calculate the differences in the time steps between consecutive times in M
. The differences are duration
values, formatted to display the time steps as hours, minutes, and seconds.
T = 4×1 duration 744:00:00 696:00:00 744:00:00 720:00:00
M
is regular with respect to months because the time interval between the row times of M
is always one calendar month.
tf = isregular(M,'months')
Create a timetable. Determine if it is regular, and then return the size of the time step if it is.
Time = [minutes(0):minutes(15):minutes(60)]'; Pulse = [72 75 80 73 69]'; TT = timetable(Time,Pulse)
TT=5×1 timetable Time Pulse ______ _____
0 min 72
15 min 75
30 min 80
45 min 73
60 min 69
TT
is a regular timetable.
Input Arguments
Input variable, specified as a timetable, a datetime
vector, or a duration
vector.
Time or calendar unit, specified as a character vector or string scalar.isregular
determines if the consecutive times ofD
are regular to the time or calendar unit specified by timeUnit
. The table lists the units that you can specify.
Time or Calendar Unit | Description |
---|---|
'years' | Regular to the year |
'quarters' | Regular to the quarter |
'months' | Regular to the month |
'weeks' | Regular to the week |
'days' | Regular to the day |
'time' (default) | Regular with respect to time |
- If
D
is adatetime
vector or a timetable whose row times aredatetime
values, then the time steps might be regular with respect to a calendar unit such as months, but irregular with respect to exact elapsed time.
For example, if the times are regular monthlydatetime
values, andtimeUnit
is'month'
, thenisregular
returns1
. But iftimeUnit
is'time'
, thenisregular
returns0
because different months can represent different lengths of time. - If
D
is aduration
vector or a timetable whose row times areduration
values, then specifytimeUnit
as'time'
or use the first syntax. Theduration
data type does not represent times using calendar units.
Output Arguments
True or false, returned as a logical 1
if the input is regular and a logical 0
if it is not.
Time step between consecutive times, returned as aduration
or calendarDuration
scalar. If the input is not regular, then dt
is aNaN
value.
Tips
- In certain cases, you can create a timetable or
datetime
vector while specifying a regular time step, and yet the result is irregular. Such a result can occur when you specify the time step by using a calendar unit of time and there is a time that introduces an irregular step. For example, if you create a timetable with a time step of one calendar month, starting on January 31, 2019, then it is irregular with respect to months.
stime = datetime(2019,1,31);
tstep = calmonths(1);
TT = timetable('Size',[3 1],'VariableTypes',{'double'},...
'TimeStep',tstep,'StartTime',stime);
tf = isregular(TT,'month')
- There are other cases where irregularities are due to shifts from Daylight Saving Time (DST) or to
datetime
values that are leap seconds. This table specifies the dates, times, and time steps that can produce irregular results unexpectedly.Row Time Value Time Step Start time specified as the 29th, 30th, or 31st day of the month. Number of calendar months or quarters. Start time specified as February 29. Number of calendar years. Any datetime value occurring between 1:00 a.m. and 2:00 a.m. on a day shifting from DST to standard time (when such values have a time zone that observes DST). Number of calendar days or months. Any datetime value that is a leap second (when the time zone for such values is the UTCLeapSeconds time zone). For the list of leap seconds, see leapseconds. Time step specified in any calendar unit (days, weeks, months, quarters, or years).
Extended Capabilities
Theisregular
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2016b
You can use the isregular
function to determine if a timetable, datetime
vector, or duration
vector is regular. In previous releases, you can use isregular
only on a timetable.