%% Dot Notation with Any Variable Name or Expression
% When you index using dot notation, there are two ways to specify a variable.
% By name, without quotation marks. For example, T.Date specifies a variable named 'Date'.
% By an expression, where the expression is enclosed by parentheses after the dot. For example, T.('Start Date') specifies a variable named 'Start Date'.
% Use the first syntax when a table variable name also happens to be a valid MATLAB® identifier. (A valid identifier starts with a letter and includes only letters, digits, and underscores.)
% Use the second syntax when you specify:
% A number that indicates the position of the variable in the table.
% A variable name that isn't a valid MATLAB identifier.
% A function whose output is the name of a variable in the table, or a variable you add to the table. The output of the function must be a character vector or a string scalar.
% For example, create a table from the patients MAT-file. Then use dot notation to access the contents of table variables.

load patients

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

%
% To specify a variable by position in the table, use a number. Age is the first variable in T, so use the number 1 to specify its position.

T.(1)

%
% To specify a variable by name, you can enclose it in quotation marks. Since 'Age' is a valid identifier, you can specify it using either T.Age or T.('Age').

T.('Age')

%
% You can specify table variable names that are not valid MATLAB identifiers. Variable names can include spaces and non-ASCII characters, and can start with any character. However, when you use dot notation to access a table variable with such a name, you must specify it using parentheses.
% Add a variable name with spaces and a hyphen to T.

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

%
% Access the new table variable using dot notation. Display the first five elements.

C = T.('Self-Assessed Health Status');
C(1:5)

%
% You also can use the output of a function as a variable name. Delete the T.('Self-Assessed Health Status') variable. Then replace it with a variable whose name includes today's date.

T.('Self-Assessed Health Status') = [];
T.(string(datetime('today')) + ' Self Report') = SelfAssessedHealthStatus;
T(1:5,:)

% Copyright 2019 The MathWorks, Inc.