%% Add, Delete, and Rearrange Table Variables
% This example shows how to add, delete, and rearrange column-oriented variables in a table. You can add, move, and delete table variables using the addvars, movevars, and removevars functions. As alternatives, you also can modify table variables using dot syntax or by indexing into the table. Use the splitvars and mergevars functions to split multicolumn variables and combine multiple variables into one. Finally, you can reorient a table so that the rows of the table become variables of an output table, using the rows2vars function.
% You also can modify table variables using the Variables Editor.
%% Load Sample Data and Create Tables
% Load arrays of sample data from the patients MAT-file. Display the names and sizes of the variables loaded into the workspace.

load patients
whos -file patients

%
% Create two tables. Create one table, T, with information collected from a patient questionnaire and create another table, T2, with data measured from patients. Each table has 100 rows.

T = table(Age,Gender,Smoker);
T2 = table(Height,Weight,Systolic,Diastolic);

%
% Display the first five rows of each table.

head(T,5)
head(T2,5)

%
%% Add Variables Concatenated from Another Table
% Add variables to the table T by horizontally concatenating it with T2.

T = [T T2];

%
% Display the first five rows of T.

head(T,5)

% The table T now has 7 variables and 100 rows.
% If the tables that you are horizontally concatenating have row names, horzcat concatenates the tables by matching the row names. Therefore, the tables must use the same row names, but the row order does not matter.
%
%% Add Variable from Workspace to Table
% Add the names of patients from the workspace variable LastName before the first table variable in T. You can specify any location in the table using the name of a variable near the new location. Use quotation marks to refer to the names of table variables. However, do not use quotation marks for input arguments that are workspace variables.

T = addvars(T,LastName,'Before','Age');
head(T,5)

% You also can specify locations in a table using numbers. For example, the equivalent syntax using a number to specify location is T = addvars(T,LastName,'Before',1).
%
%% Add Variables Using Dot Syntax
% An alternative way to add new table variables is to use dot syntax. When you use dot syntax, you always add the new variable as the last table variable. You can add a variable that has any data type, as long as it has the same number of rows as the table.
% Create a new variable for blood pressure as a horizontal concatenation of the two variables Systolic and Diastolic. Add it to T.

T.BloodPressure = [Systolic Diastolic];
head(T,5)

% T now has 9 variables and 100 rows. A table variable can have multiple columns. So although BloodPressure has two columns, it is one table variable.
%
% Add a new variable, BMI, in the table T, that contains the body mass index for each patient. BMI is a function of height and weight. When you calculate BMI, you can refer to the Weight and Height variables that are in T.

T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;

% The operators ./ and .^ in the calculation of BMI indicate element-wise division and exponentiation, respectively.
%
% Display the first five rows of the table T.

head(T,5)

%
%% Move Variable in Table
% Move the table variable BMI using the movevars function, so that it is after the variable Weight. When you specify table variables by name, use quotation marks.

T = movevars(T,'BMI','After','Weight');
head(T,5)

% You also can specify locations in a table using numbers. For example, the equivalent syntax using a number to specify location is T = movevars(T,'BMI,'After',6). It is often more convenient to refer to variables by name.
%
%% Move Table Variable Using Indexing
% As an alternative, you can move table variables by indexing. You can index into a table using the same syntax you use for indexing into a matrix.
% Move BloodPressure so that it is next to BMI.

T = T(:,[1:7 10 8 9]);
head(T,5)

% In a table with many variables, it is often more convenient to use the movevars function.
%
%% Delete Variables
% To delete table variables, use the removevars function. Delete the Systolic and Diastolic table variables.

T = removevars(T,{'Systolic','Diastolic'});
head(T,5)

%
%% Delete Variable Using Dot Syntax
% As an alternative, you can delete variables using dot syntax and the empty matrix, []. Remove the Age variable from the table.

T.Age = [];
head(T,5)

%
%% Delete Variable Using Indexing
% You also can delete variables using indexing and the empty matrix, []. Remove the Gender variable from the table.

T(:,'Gender') = [];
head(T,5)

%
%% Split and Merge Table Variables
% To split multicolumn table variables into variables that each have one column, use the splitvars functions. Split the variable BloodPressure into two variables.

T = splitvars(T,'BloodPressure','NewVariableNames',{'Systolic','Diastolic'});
head(T,5)

% Similarly, you can group related table variables together in one variable, using the mergevars function. Combine Systolic and Diastolic back into one variable, and name it BP.

T = mergevars(T,{'Systolic','Diastolic'},'NewVariableName','BP');
head(T,5)

%
%% Reorient Rows To Become Variables
% You can reorient the rows of a table or timetable, so that they become the variables the output table, using the rows2vars function. However, if the table has multicolumn variables, then you must split them before you can call rows2vars.
% Reorient the rows of T. Specify that the names of the patients in T are the names of table variables in the output table. The first variable of T3 contains the names of the variables of T. Each remaining variable of T3 contains the data from the corresponding row of T.

T = splitvars(T,'BP','NewVariableNames',{'Systolic','Diastolic'});
T3 = rows2vars(T,'VariableNamesSource','LastName');
T3(:,1:5)

% You can use dot syntax with T3 to access patient data as an array. However, if the row values of an input table cannot be concatenated, then the variables of the output table are cell arrays.

T3.Smith

% Copyright 2015 The MathWorks, Inc.