Select all the data for the patients with the last names 'Smith' and 'Johnson'. In this
case, it is simpler to use the row names than to use numeric indices. Tnew is a 2-by-5
table.
Tnew = T({'Smith','Johnson'},:)
Tnew=2×5 table
Gender Smoker Height Weight ID
Smith 'Male' true 71 176 8148
Johnson 'Male' false 69 163 9058
Select the height and weight of the patient named 'Johnson' by indexing on variable
names. Tnew is a 1-by-2 table.
Tnew = T('Johnson',{'Height','Weight'})
Tnew=1×2 table
Height Weight
Johnson 69 163
You can access table variables either with dot syntax, as in T.Height, or by named
indexing, as in T(:,'Height').
Calculate and Add Result as Table Variable
You can access the contents of table variables, and then perform calculations on them
using MATLAB® functions. Calculate body-mass-index (BMI) based on data in the existing
table variables and add it as a new variable. Plot the relationship of BMI to a patient's
status as a smoker or a nonsmoker. Add blood-pressure readings to the table, and plot the
relationship of blood pressure to BMI.
Calculate BMI using the table variables, Weight and Height. You can extract Weight
and Height for the calculation while conveniently keeping Weight, Height, and BMI in
the table with the rest of the patient data. Display the first five rows of T.
T.BMI = (T.Weight0.453592)./(T.Height0.0254).^2;
T(1:5,:)
Create and Work with Tables