%% Store Related Data Variables in Table
% Store data about a group of patients in a table. You can perform calculations and store results in the same table. Also, you can annotate the table to describe your work and the variables of the table.
% First, create workspace variables that have the patient data. The variables can have any data types but must have the same number of rows.

LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

%
% Create a table, T, as a container for the workspace variables. The table function uses the workspace variable names as the names of the table variables in T. A table variable can have multiple columns. For example, the BloodPressure variable in T is a 5-by-2 array.

T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)

%
% You can use dot indexing to access table variables. For example, calculate the mean height of the patients using the values in T.Height.

meanHeight = mean(T.Height)

%
% Calculate body mass index (BMI), and add it as a new table variable. You also can add and name table variables in one step, using dot syntax.

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

%
% Annotate the table with a description of the BMI calculation. You can annotate T and its variables using metadata accessed through T.Properties.

T.Properties.Description = 'Patient data, including body mass index (BMI) calculated using Height and Weight';
T.Properties

% Copyright 2012 The MathWorks, Inc.