%% Create and Modify Table Properties
% This example shows how to create and modify table properties. Meant only to provide examplesectionobjects for the table properties page. Not meant to be published on its own.
%% Create First Table
% Create a table with default variable names. Then modify the names using the Properties.VariableNames property.

T = table({'M';'M';'F';'F';'F'},[38;43;38;40;49], ...
          [71;69;64;67;64],[176;163;131;133;119])

%
%% Modify Variable Names

T.Properties.VariableNames = {'Gender','Age','Height','Weight'}

% A fundamental way to display and modify variables is to access them by name using dot syntax.

T.Age
T.Age(1) = 53

%
%% Subscript on Variable Names
% Create a subtable. Include all rows, but subscript on Age and Gender.

T(:,{'Age','Gender'})

%
%% Extract Data with Subscripts
% Extract data from T. Use variable names within curly braces to extract the data from the variables Height and Weight.

T{:,{'Height','Weight'}}

%
%% Extract Data with Dot Indexing
% Extract data from the variable Gender using dot indexing.

T.Gender

%
%% Extract All Data
% Extract all data using the Variables property. T.Variables is equivalent to T{:,:}. However, this syntax concatenates the table variables together. The data types of the table variables must be data types that can be concatenated.
% Copy T to T2, and delete T2.Gender. The remaining variables of T2 are numeric. Concatenate them together into a numeric array using T2.Variables.

T2 = T;
T2.Gender = [];
T2.Variables

%
%% Create Second Table
% Create a table. Then add row names and access rows by their names.

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

%
%% Add Row Names
% Add row names using the Properties.RowNames property. By default, tables do not have row names, but you can add them at any time.

T.Properties.RowNames = LastName;
head(T,4)

% Another way to access the row names is by using dot syntax with the name of the first dimension of the table. Display the first five row names.

T.Properties.DimensionNames
T.Row(1:5)

%
%% Subscript by Row Names
% Index into the table by row names.

T({'Smith','Williams'},:)

%
%% Modify Dimension Names
% Create a table and display its dimension names. You can access row names and data using dimension names with dot syntax.

load patients
T = table(Age,Height,Weight,Systolic,Diastolic, ...
          'RowNames',LastName);
T.Properties.DimensionNames

% Access the row names using the first dimension name. Display the first five names.

T.Row(1:5)

% Access the data using the second dimension name. This syntax is equivalent to T{:,:}.

T.Variables

% Modify the names of its dimensions using the Properties.DimensionNames property. Having changed the dimension names, you can access the row names and data using the syntaxes T.Patient and T.Data respectively.

T.Properties.DimensionNames = {'Patient','Data'};
T.Properties

%
%% Modify Description
% Create a table. Modify the description of the table. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight);
T.Properties.Description = 'Simulated patient data';
summary(T)

%
%% Modify Variable Descriptions
% Create a table. Modify the variable descriptions. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
T.Properties.VariableDescriptions = {'','','','', ...
                                     'Has the patient ever been a smoker', ...
                                     'Systolic Pressure','Diastolic Pressure'};
summary(T)

%
%% Modify Variable Units
% Create a table. Modify the variable units. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
T.Properties.VariableUnits = {'','Yrs','In','Lbs','','mm Hg','mm Hg'};
summary(T)

%
%% Modify User Data
% Create a table. Attach an anonymous function as a piece of user data that is associated with the table.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
formula = @(x) x.^2;
T.Properties.UserData = formula;
T.Properties

%
%% Add Customized Metadata
% Create a table.

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

%
% Add properties that can hold customized metadata about the table and its variables. In this example, the metadata are names of instruments, true and false values indicating whether variables are to be plotted, and the name of an output file. To add properties, use the addprop function.

T = addprop(T,{'Instrument','ToPlot','OutputFile'},{'variable','variable','table'});
T.Properties

%
% Assign values to the customized metadata using dot syntax. When you assign an array of text values to customized metadata, the best practice is to use a string array, not a cell array of character vectors. If a property of CustomProperties is a cell array of character vectors, then there is no mechanism to prevent you from later assigning nontext values as elements of the cell array.

T.Properties.CustomProperties.Instrument = ["","","height rod","scale","","blood pressure cuff","blood pressure cuff"];
T.Properties.CustomProperties.ToPlot = [false false true true false true true];
T.Properties.CustomProperties.OutputFile = 'patientData.csv';
T.Properties

%
%% Remove Customized Metadata
% Remove the OutputFile property from T.

T = rmprop(T,'OutputFile');
T.Properties

% Copyright 2019 The MathWorks, Inc.