Time tolerance for timetable row subscripting
S = withtol( creates
a subscript to select rows of a timetable.
all rows whose row times match a time in
the tolerance specified by
is a datetime or duration array, or a cell array of character vectors
that specify dates and times.
tol must be a duration
rowTimes contains datetime values, then
you can only use
S to subscript into a timetable
whose row times are datetime values. Similarly, if
duration values, then you can only use
S to subscript
into a timetable whose row times are duration values.
S = withtol(
creates a row subscript using an event filter. To use event filters, the timetable
you subscript into must have an attached event table. For more information, see
eventfilter. (since R2023a)
Specify Tolerance to Match Row Times
Create a timetable that contains temperature and pressure data with row times in hours. There is a slight random variance in the row times. Select rows with row times that match corresponding times in a time vector within a tolerance of five seconds.
Time = datetime(2015,12,18) + hours(1:10)' + seconds(randn(10,1)); Temp = [37.3 39.1 42.3 42.6 43 43.9 44.1 43.3 42.5 42]'; Pressure = [29.4 29.6 30.0 30.0 30.1 29.9 29.9 29.8 29.6 29.7]'; TT = timetable(Time,Temp,Pressure)
TT=10×2 timetable Time Temp Pressure ____________________ ____ ________ 18-Dec-2015 01:00:00 37.3 29.4 18-Dec-2015 02:00:01 39.1 29.6 18-Dec-2015 02:59:57 42.3 30 18-Dec-2015 04:00:00 42.6 30 18-Dec-2015 05:00:00 43 30.1 18-Dec-2015 05:59:58 43.9 29.9 18-Dec-2015 06:59:59 44.1 29.9 18-Dec-2015 08:00:00 43.3 29.8 18-Dec-2015 09:00:03 42.5 29.6 18-Dec-2015 10:00:02 42 29.7
Create a time vector spanning the hours from 3:00 to 8:00.
newTimes = datetime(2015,12,18) + hours(3:8)
newTimes = 1x6 datetime 18-Dec-2015 03:00:00 18-Dec-2015 04:00:00 18-Dec-2015 05:00:00 18-Dec-2015 06:00:00 18-Dec-2015 07:00:00 18-Dec-2015 08:00:00
Select rows of
TT with row times that match times in
newTimes within five seconds.
S = withtol(newTimes,seconds(5)); TT2 = TT(S,:)
TT2=6×2 timetable Time Temp Pressure ____________________ ____ ________ 18-Dec-2015 02:59:57 42.3 30 18-Dec-2015 04:00:00 42.6 30 18-Dec-2015 05:00:00 43 30.1 18-Dec-2015 05:59:58 43.9 29.9 18-Dec-2015 06:59:59 44.1 29.9 18-Dec-2015 08:00:00 43.3 29.8
rowTimes — Times to match in timetable
datetime array | duration array | cell array of character vectors | string array
Times to match in a timetable, specified as a datetime array, duration array, cell array of
character vectors, or string array.
times that do not exactly match times in the row times of a timetable, but
that might be within a specified tolerance.
rowTimes is a cell array of character vectors or string array, then the
elements of the array specify dates and times that the
tol — Tolerance for matching times to row times of timetable
duration | character vector | string scalar
Tolerance for matching times to the row times of a timetable, specified as a duration, character vector, or string scalar.
tol is a character vector or string scalar, then
tol specifies a time that the
duration function can convert.
EF — event filter
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version HistoryIntroduced in R2016b
R2023a: Improved performance when subscripting into timetable with
timetable subscripting when subscripting with a
withtol subscript is significantly faster in R2023a than in
When you use a
withtolsubscript with a timetable that has 107 rows, performance in R2023a is about 44x faster than in R2022b.
function timingTest() rng default % 10^7 rows N = 10000000; rowtimes = seconds(0:N-1); tt = timetable(rand(N,1),RowTimes=rowtimes); % 100 values chosen in steps of 10 n = 1000; t = seconds(0:10:n-1); tt.Time = tt.Time + .1*seconds(rand(N,1)); wt = withtol(t,seconds(.1)); tic tt2 = tt(wt,:); toc end
The approximate execution times are:
R2022b: 3.92 s
R2023a: 0.09 s
The code was timed on a Windows® 10, AMD® EPYC 74F3 24-Core Processor @ 3.19 GHz test system by calling each
version of the