MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Index on Specified Times with Tolerances

Specify a tolerance when indexing on times. You can use the withtol function to create a
subscript as a helper. With the output of withtol, you can select row times that match
within the specified tolerance.

Index into TT on specified dates. Specify a tolerance of one day to return rows with row
times that are within one day of the specified dates. The times must be in a datetime or
duration vector, or in a cell array of character vectors that can be converted to datetime
or duration values. The tolerance must be specified as a duration, using a function such
as seconds, minutes, hours, or days.

rowTimes = {'2002-02-01','2003-02-07'};
S = withtol(rowTimes,days(1));
TT(S,:)

ans=2×6 timetable
OutageTime Region Loss Customers RestorationTime Cause
________________ _________ ______ __________ ________________ ____________

2002-02-01 12:18 SouthWest 458.98 1.8202e+06 2002-02-07 16:50 winter storm
2003-02-07 21:15 SouthEast 289.4 1.4294e+05 2003-02-17 08:14 winter storm

Subscript by Variable Type

To select all timetable variables that are of a given type, create a subscript as a helper,
using the vartype function. You can specify variable types without having to specify their
names or positions in the timetable.

Select all variables that contain numeric data. TT2 contains only the variables Loss and
Customers. The other three variables of TT are either categorical or datetime variables.
Display the first five rows of TT2.

S = vartype('numeric');
TT2 = TT(:,S);
TT2(1:5,:)

ans=5×3 timetable
OutageTime Loss Customers
________________ ______ __________

2002-02-01 12:18 458.98 1.8202e+06
2003-01-23 00:49 530.14 2.1204e+05

10 Timetables

Free download pdf