%% Convert Text in Table Variables to Categorical
% This example shows how to convert a variable in a table from a cell array of character vectors to a categorical array. The same workflow applies for table variables that are string arrays.
%% Load Sample Data and Create a Table
% Load sample data gathered from 100 patients.

load patients

whos

%
% Store the patient data from Age, Gender, Height, Weight, SelfAssessedHealthStatus, and Location in a table. Use the unique identifiers in the variable LastName as row names.

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

%
%% Convert Table Variables from Cell Arrays of Character Vectors to Categorical Arrays
% The cell arrays of character vectors, Gender and Location, contain discrete sets of unique values.
% Convert Gender and Location to categorical arrays.

T.Gender = categorical(T.Gender);
T.Location = categorical(T.Location);

%
% The variable, SelfAssessedHealthStatus, contains four unique values: Excellent, Fair, Good, and Poor.
% Convert SelfAssessedHealthStatus to an ordinal categorical array, such that the categories have the mathematical ordering Poor < Fair < Good < Excellent.

T.SelfAssessedHealthStatus = categorical(T.SelfAssessedHealthStatus,...
    {'Poor','Fair','Good','Excellent'},'Ordinal',true);

%
%% Print a Summary
% View the data type, description, units, and other descriptive statistics for each variable by using summary to summarize the table.

format compact

summary(T)

% The table variables Gender, SelfAssessedHealthStatus, and Location are categorical arrays. The summary contains the counts of the number of elements in each category. For example, the summary indicates that 53 of the 100 patients are female and 47 are male.
%
%% Select Data Based on Categories
% Create a subtable, T1, containing the age, height, and weight of all female patients who were observed at County General Hospital. You can easily create a logical vector based on the values in the categorical arrays Gender and Location.

rows = T.Location=='County General Hospital' & T.Gender=='Female';

% rows is a 100-by-1 logical vector with logical true (1) for the table rows where the gender is female and the location is County General Hospital.
%
% Define the subset of variables.

vars = {'Age','Height','Weight'};

%
% Use parentheses to create the subtable, T1.

T1 = T(rows,vars)

% A is a 19-by-3 table.
%
% Since ordinal categorical arrays have a mathematical ordering for their categories, you can perform element-wise comparisons of them with relational operations, such as greater than and less than.
% Create a subtable, T2, of the gender, age, height, and weight of all patients who assessed their health status as poor or fair.
% First, define the subset of rows to include in table T2.

rows = T.SelfAssessedHealthStatus<='Fair';

%
% Then, define the subset of variables to include in table T2.

vars = {'Gender','Age','Height','Weight'};

%
% Use parentheses to create the subtable T2.

T2 = T(rows,vars)

% T2 is a 26-by-4 table.
% Copyright 2012-2020 The MathWorks, Inc.