Rename and Describe Table Variables
Tables have data in column-oriented variables. But tables also provide properties that can store more descriptive information about the table and its variables. For example, the variable names are properties. You might want to change them to be more descriptive. To change variable names, use the renamevars
function. Tables have other properties such as descriptions of the variables and units associated with variables. A table stores its properties in a Properties
object. Add descriptions and units to the Properties
object by using dot notation. You can also produce a summary of a table that combines statistics about the table variables with their names, descriptions, and units. To produce this summary, use the summary
function.
Create Table from Sample Data
Load the sample patient data from the file patients.mat
. The file contains arrays that have different data types.
load patients.mat
whos
Name Size Bytes Class Attributes Age 100x1 800 double Diastolic 100x1 800 double Gender 100x1 11412 cell Height 100x1 800 double LastName 100x1 11616 cell Location 100x1 14208 cell SelfAssessedHealthStatus 100x1 11540 cell Smoker 100x1 100 logical Systolic 100x1 800 double Weight 100x1 800 double
Create a table from a subset of these arrays. If you need to modify the input arrays, you can modify them before creating the table. For example, combine Systolic
and Diastolic
into a 100-by-2 matrix. Then convert LastName
to a string array.
BloodPressure = [Systolic Diastolic]; LastName = string(LastName);
To create a table from input arrays, use the table
function.
T = table(LastName,Age,Height,Weight,Smoker,BloodPressure)
T=100×6 table
LastName Age Height Weight Smoker BloodPressure
__________ ___ ______ ______ ______ _____________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
Access Table Properties
Table variables have names. These names are properties of the table that enable you to access the data stored in the variables. But a table also has other properties. Some properties enable you to describe the table as a whole. Other properties enable you to describe the table variables.
A table stores its properties in a Properties
object. To access the properties of a table, use dot notation.
T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'LastName' 'Age' 'Height' 'Weight' 'Smoker' 'BloodPressure'} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
You can also use dot notation to access a specific property. For example, access the set of variable names.
T.Properties.VariableNames
ans = 1x6 cell
{'LastName'} {'Age'} {'Height'} {'Weight'} {'Smoker'} {'BloodPressure'}
Rename Table Variables
It is helpful when variable names are descriptive. So, you might want to rename variables in your table.
The recommended way to rename variables is to use the renamevars
function. For example, rename the LastName
variable of T
to PatientName
.
T = renamevars(T,"LastName","PatientName")
T=100×6 table
PatientName Age Height Weight Smoker BloodPressure
___________ ___ ______ ______ ______ _____________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
Another way to rename variables is to access the T.Properties.VariableNames
property. For example, rename the BloodPressure
variable.
T.Properties.VariableNames("BloodPressure") = "BP"
T=100×6 table
PatientName Age Height Weight Smoker BP
___________ ___ ______ ______ ______ __________
"Smith" 38 71 176 true 124 93
"Johnson" 43 69 163 false 109 77
"Williams" 38 64 131 false 125 83
"Jones" 40 67 133 false 117 75
"Brown" 49 64 119 false 122 80
"Davis" 46 68 142 false 121 70
"Miller" 33 64 142 true 130 88
"Wilson" 40 68 180 false 115 82
"Moore" 28 68 183 false 115 78
"Taylor" 31 66 132 false 118 86
"Anderson" 45 68 128 false 114 77
"Thomas" 42 66 137 false 115 68
"Jackson" 25 71 174 false 127 74
"White" 39 72 202 true 130 95
"Harris" 36 65 129 false 114 79
"Martin" 48 71 181 true 130 92
⋮
Edit Other Properties
To edit any other table property, you must use dot notation. In general, the other properties enable you to annotate the table with information that describes it or the variables.
For example, add a string array that lists the units associated with the table variables. Assign it to the VariableUnits
property. While the property is a cell array of character vectors, you can assign values to it using a string array. An individual empty string within the string array indicates that the corresponding variable does not have units.
T.Properties.VariableUnits = ["","Yrs","In","Lbs","","mm Hg"]; T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableDescriptions: {} VariableUnits: {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
You can also assign values by indexing into properties. For example, add descriptions for the PatientName
and BP
variables only. You can index by name or by the position a variable has in the table.
T.Properties.VariableDescriptions(1) = "Patient last name"; T.Properties.VariableDescriptions("BP") = "Systolic/Diastolic"; T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableDescriptions: {'Patient last name' '' '' '' '' 'Systolic/Diastolic'} VariableUnits: {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
Remove Properties
You cannot remove table properties. However, you can delete the values stored in a table property.
Remove the description for the LastName
variable. The descriptions are text, so remove it by assigning an empty string as the new description.
T.Properties.VariableDescriptions(1) = "";
T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableDescriptions: {'' '' '' '' '' 'Systolic/Diastolic'} VariableUnits: {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
Remove all the descriptions in VariableDescriptions
. To remove all the values stored in a table property, assign an empty array.
If the property stores text in a cell array, assign
{}
.If the property stores numeric or other types of values in an array, assign
[]
.
T.Properties.VariableDescriptions = {}; T.Properties
ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableDescriptions: {} VariableUnits: {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
Summarize Table Variable Data and Properties
You can produce a summary of a table that combines its properties with statistics about each variable. To produce this summary, use the summary
function.
First, add variable descriptions back to T
, along with a description of the table.
T.Properties.Description = "Table of Data for 100 Patients"; T.Properties.VariableDescriptions = ["Patient name","","","","True if patient smokes","Systolic and diastolic readings"]; T.Properties
ans = TableProperties with properties: Description: 'Table of Data for 100 Patients' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {'PatientName' 'Age' 'Height' 'Weight' 'Smoker' 'BP'} VariableDescriptions: {'Patient name' '' '' '' 'True if patient smokes' 'Systolic and diastolic readings'} VariableUnits: {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg'} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
Next, call summary
. The summary displays the description of the table and the descriptions and units for each variable. Also, summary
produces statistics for table variables whose data types support the required calculations.
summary(T)
Description: Table of Data for 100 Patients Variables: PatientName: 100x1 string Properties: Description: Patient name Age: 100x1 double Properties: Units: Yrs Values: Min 25 Median 39 Max 50 Height: 100x1 double Properties: Units: In Values: Min 60 Median 67 Max 72 Weight: 100x1 double Properties: Units: Lbs Values: Min 111 Median 142.5 Max 202 Smoker: 100x1 logical Properties: Description: True if patient smokes Values: True 34 False 66 BP: 100x2 double Properties: Units: mm Hg Description: Systolic and diastolic readings Values: Column 1 Column 2 ________ ________ Min 109 68 Median 122 81.5 Max 138 99
You can also store the summary in a structure instead of displaying it.
S = summary(T)
S = struct with fields:
PatientName: [1x1 struct]
Age: [1x1 struct]
Height: [1x1 struct]
Weight: [1x1 struct]
Smoker: [1x1 struct]
BP: [1x1 struct]
Each field of S
contains a description of a variable of T
.
S.BP
ans = struct with fields:
Size: [100 2]
Type: 'double'
Description: 'Systolic and diastolic readings'
Units: 'mm Hg'
Continuity: []
Min: [109 68]
Median: [122 81.5000]
Max: [138 99]
NumMissing: [0 0]
See Also
table
| renamevars
| summary