MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Populate a table with column-oriented variables that contain patient data. You can access
and assign table variables by name. When you assign a table variable from a workspace
variable, you can assign the table variable a different name.


Create a table and populate it with the Gender, Smoker, Height, and Weight workspace
variables. Display the first five rows.


T = table(Gender,Smoker,Height,Weight);
T(1:5,:)


ans=5×4 table
Gender Smoker Height Weight




'Male' true 71 176
'Male' false 69 163
'Female' false 64 131
'Female' false 67 133
'Female' false 64 119


As an alternative, use the readtable function to read the patient data from a comma-
delimited file. readtable reads all the columns that are in a file.


Create a table by reading all columns from the file, patients.dat.


T2 = readtable('patients.dat');
T2(1:5,:)


ans=5×10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus




'Smith' 'Male' 38 'County General Hospital' 71 176 1 124 93 'Excellent'
'Johnson' 'Male' 43 'VA Hospital' 69 163 0 109 77 'Fair'
'Williams' 'Female' 38 'St. Mary's Medical Center' 64 131 0 125 83 'Good'
'Jones' 'Female' 40 'VA Hospital' 67 133 0 117 75 'Fair'
'Brown' 'Female' 49 'County General Hospital' 64 119 0 122 80 'Good'


You can assign more column-oriented table variables using dot notation, T.varname,
where T is the table and varname is the desired variable name. Create identifiers that are
random numbers. Then assign them to a table variable, and name the table variable ID.
All the variables you assign to a table must have the same number of rows. Display the
first five rows of T.


Create and Work with Tables
Free download pdf