timerange
Selects the rows of a timetable that fall within a time interval.
Category: Language Fundamentals > Data Types
Syntax
S = timerange(t1, t2)
Subscript that selects row times from t1 up to but not including t2.
S = timerange(t1, t2, intervalType)
Chooses which ends are included: 'openright' (default), 'closed', 'open' or 'openleft'.
Description
timerange returns a subscript object, not data. It only has meaning inside the parentheses of a timetable. By default the interval is closed on the left and open on the right, so timerange(seconds(2), seconds(3)) selects the row at 2 s but not the one at 3 s. Pass 'closed' to include both ends.
Input arguments
t1, t2- Start and end of the interval, as datetime or duration values matching the row times.
intervalType- Which of the two ends belong to the interval.
Output arguments
S- Time-based subscript to use in parentheses with a timetable, e.g.
TT(timerange(a, b), :).
Example
Open right end and closed interval
t = datetime(2024,1,1) + days(0:4)';
TT = timetable(t, (1:5)', 'VariableNames', {'v'});
TT(timerange(datetime(2024,1,2), datetime(2024,1,4)), :)
TT(timerange(datetime(2024,1,2), datetime(2024,1,4), 'closed'), :)
Output
ans = 2×1 timetable
t v
___________ _
02-Jan-2024 2
03-Jan-2024 3
ans = 3×1 timetable
t v
___________ _
02-Jan-2024 2
03-Jan-2024 3
04-Jan-2024 4