%% Select Timetable Data by Row Time and Variable Type
% A timetable is a type of table that associates a time with each row. You can subscript into a timetable to select subsets of its data in a number of different ways. To select timetable rows with row times that fall within a given time range, specify the time range using the timerange function. Since a timetable is a table, you can index on rows and variables using either smooth parentheses or curly braces. You can index on specific row times, or select rows with row times that match specified times within a tolerance you set using the withtol function. You can also subscript into a table or timetable to select all the variables that match a type you specify with the vartype function. Finally, extract data from a timetable into a matrix using the Variables property.
%% Create Timetable from File
% Create a timetable from the sample file outages.csv, containing data representing electric utility outages in the United States. Read the table from the file with the readtable function. Convert T.Cause and T.Region into categorical arrays. Then convert the table to a timetable using the  table2timetable function. Display the first five rows of the timetable.  TT is a timetable containing outage data from February 2002 to January 2014.

T = readtable('outages.csv');
T.Cause = categorical(T.Cause);
T.Region = categorical(T.Region);
TT = table2timetable(T);
TT(1:5,:)

%
%% Summarize Timetable and Access Row Times
% Display a summary of TT. It is a timetable that contains 1468 rows and five variables.

summary(TT)

%
% Access the row times. The row times are not in a variable. Instead, the vector of row times is a property of the timetable. However, you can access the row times with dot syntax. TT.OutageTime is a 1468-by-1 vector of datetime values. Display the first five rows of TT.OutageTime.

TT.OutageTime(1:5)

%
%% Subscript on Time Range
% To select all timetable rows that fall in a time range, create a subscript as a helper, using the timerange function. The start and end times you specify do not have to match any of the row times in the timetable.
% Select all rows with outages that occurred between January 2002 and December 2003. Display the first five rows of TT2.

TR = timerange('2002-01-01','2003-12-31');
TT2 = TT(TR,:);
TT2(1:5,:)

%
% Display the last five rows of TT2.

TT2(end-4:end,:)

% TT2 is a timetable with only 98 rows, containing outage data only from 2002 and 2003.
%
%% Index on Specified Times
% You can index into TT with datetime values or character vectors representing specific times in TT.OutageTime. But when you do so, the times you specify must have exact matches in the time vector, and only those times are selected. Index into TT on times for the first and third rows of TT.

TT({'2002-02-01 12:18:00','2003-02-07 21:15:00'},:)

%
%% 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,:)

%
%% 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,:)

%
% Subscript both on a time range and by variable type.

TR = timerange('2002-01-01','2003-12-31');
TT2 = TT(TR,S);
TT2(1:5,:)

%
%% Extract Data with Variables Property
% Tables and timetables have a property, Variables, that you can use to extract data from variables into a matrix, as long as the variables can be concatenated together.
% Extract the numeric data from TT2 using the Variables property. A is a 1468-by-2 matrix of doubles. When you extract data from a timetable into an array, the row times are not included.

A = TT2.Variables;
A(1:5,:)

%
% The result of TT2.Variables is the same as the result of using curly braces to extract data, using the TT2{:,:} syntax.
% You can concatenate the variables in TT2 into an array of doubles. However, TT contains numeric, categorical, and datetime variables that cannot be concatenated. The Variables property returns an error when variables cannot be concatenated. To avoid such an error, you can subscript by variable type before using the Variables property.
% Subscript into TT to select numeric variables and extract them into a matrix.

A = TT(:,vartype('numeric')).Variables;
A(1:5,:)

% Copyright 2012 The MathWorks, Inc.