MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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.

Load Sample Data and Create a Table

Load sample data gathered from 100 patients.

load patients

whos

Name Size Bytes Class Attributes

Age 100x1 800 double
Diastolic 100x1 800 double
Gender 100x1 12212 cell
Height 100x1 800 double
LastName 100x1 12416 cell
Location 100x1 15008 cell
SelfAssessedHealthStatus 100x1 12340 cell
Smoker 100x1 100 logical
Systolic 100x1 800 double
Weight 100x1 800 double

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);

Convert Text in Table Variables to Categorical
Free download pdf