If the tables that you are horizontally concatenating have row names, horzcat
concatenates the tables by matching the row names. Therefore, the tables must use the
same row names, but the row order does not matter.
Add Variable from Workspace to Table
Add the names of patients from the workspace variable LastName before the first table
variable in T. You can specify any location in the table using the name of a variable near
the new location. Use quotation marks to refer to the names of table variables. However,
do not use quotation marks for input arguments that are workspace variables.
T = addvars(T,LastName,'Before','Age');
head(T,5)
ans=5×8 table
LastName Age Gender Smoker Height Weight Systolic Diastolic
__________ ___ ________ ______ ______ ______ ________ _________
'Smith' 38 'Male' true 71 176 124 93
'Johnson' 43 'Male' false 69 163 109 77
'Williams' 38 'Female' false 64 131 125 83
'Jones' 40 'Female' false 67 133 117 75
'Brown' 49 'Female' false 64 119 122 80
You also can specify locations in a table using numbers. For example, the equivalent
syntax using a number to specify location is T = addvars(T,LastName,'Before',1).
Add Variables Using Dot Syntax
An alternative way to add new table variables is to use dot syntax. When you use dot
syntax, you always add the new variable as the last table variable. You can add a variable
that has any data type, as long as it has the same number of rows as the table.
Create a new variable for blood pressure as a horizontal concatenation of the two
variables Systolic and Diastolic. Add it to T.
T.BloodPressure = [Systolic Diastolic];
head(T,5)
ans=5×9 table
LastName Age Gender Smoker Height Weight Systolic Diastolic BloodPressure
__________ ___ ________ ______ ______ ______ ________ _________ _____________
9 Tables