%% Tables Containing Specified Rows and Variables
% Load sample data for 100 patients from the patients MAT-file to workspace variables.

load patients
whos

%
% Create a table and populate it with the Age, Gender, Height, Weight, and Smoker workspace variables. Use the unique identifiers in LastName as row names. T is a 100-by-5 table. (When you specify row names, they do not count as a table variable).

T = table(Age,Gender,Height,Weight,Smoker,...
    'RowNames',LastName)

%
%% Index Using Numeric Indices
% Create a subtable containing the first five rows and all the variables from T. To specify the desired rows and variables, use numeric indices within parentheses. This type of indexing is similar to indexing into numeric arrays.

T1 = T(1:5,:)

% T1 is a 5-by-5 table.
% In addition to numeric indices, you can use row or variable names inside the parentheses. (In this case, using row indices and a colon is more compact than using row or variable names.)
%
%% Index Using Names
% Select all the data for the patients with the last names 'Williams' and 'Brown'. Since T has row names that are the last names of patients, index into T using row names.

T2 = T({'Williams','Brown'},:)

% T2 is a 2-by-5 table.
%
% You also can select variables by name. Create a table that has only the first five rows of T and the Height and Weight variables. Display it.

T3 = T(1:5,{'Height','Weight'})

%
% Table variable names do not have to be valid MATLAB identifiers. They can include spaces and non-ASCII characters, and can start with any character.
% Add a variable name with spaces and a dash to T. Then index into T using variable names.

T = addvars(T,SelfAssessedHealthStatus,'NewVariableNames','Self-Assessed Health Status');
T(1:5,{'Age','Smoker','Self-Assessed Health Status'})

%
%% Specify Data Type Subscript
% Instead of specifying variables using names or numbers, you can create a data type subscript that matches all variables having the same data type.
% First, create a data type subscript to match numeric table variables.

S = vartype('numeric')

%
% Create a table that has only the numeric variables, and only the first five rows, from T.

T4 = T(1:5,S)

% Copyright 2015 The MathWorks, Inc.