Add variables to table or timetable
collapse all in page
Syntax
T2 = addvars(T1,var1,...,varN)
T2 = addvars(T1,var1,...,varN,'After',location)
T2 = addvars(T1,var1,...,varN,'Before',location)
T2 = addvars(___,'NewVariableNames',newNames)
Description
example
T2 = addvars(T1,var1,...,varN)
adds the arrays specified by var1,…,varN
as new variables to the right of the last variable in T1
. The input arrays var1,…,varN
can be arrays having any data type, tables, and timetables. All input arguments must have the same number of rows as T1
.
For example, to add a column vector named A
after the last variable in T1
, use T2 = addvars(T1,A)
.
example
T2 = addvars(T1,var1,...,varN,'After',location)
inserts the variables to the right of the table variable indicated by location
. You can specify location
as a variable name, or a numeric or logical index.
For example, to insert a column vector named A
after table variable var2
, use T2 = addvars(T1,A,'After','var2')
.
example
T2 = addvars(T1,var1,...,varN,'Before',location)
inserts the variables to the left of the table variable indicated by location
.
For example, to insert a column vector named A
before table variable var3
, use T2 = addvars(T1,A,'Before','var3')
.
example
T2 = addvars(___,'NewVariableNames',newNames)
renames the added variables in T2
using the names specified by newNames
. The number of names in newNames
must be the same as the number of added variables. You can use this syntax with any of the input arguments of the previous syntaxes.
Examples
collapse all
Add Variables
Open Live Script
Create a table. Then add variables from the workspace to the table.
Load arrays from the patients.mat
file. Create a table that contains the names, ages, heights, and weights of patients. Then display the first three rows.
load patientsT1 = table(LastName,Age,Height,Weight);head(T1,3)
LastName Age Height Weight ____________ ___ ______ ______ {'Smith' } 38 71 176 {'Johnson' } 43 69 163 {'Williams'} 38 64 131
Add the workspace variables, Gender
and Smoker
, to the table.
T2 = addvars(T1,Gender,Smoker);head(T2,3)
LastName Age Height Weight Gender Smoker ____________ ___ ______ ______ __________ ______ {'Smith' } 38 71 176 {'Male' } true {'Johnson' } 43 69 163 {'Male' } false {'Williams'} 38 64 131 {'Female'} false
Insert Variables at Specified Locations
Open Live Script
Create a table. Then insert variables before and after specified locations in the table.
Load arrays from the patients.mat
file. Create a table that contains the names and genders of patients. Then display the first three rows.
load patientsT1 = table(LastName,Gender);head(T1,3)
LastName Gender ____________ __________ {'Smith' } {'Male' } {'Johnson' } {'Male' } {'Williams'} {'Female'}
Insert the workspace variable, Age
, before the table variable, Gender
. To refer to a table variable by name, specify its name as a character vector.
T2 = addvars(T1,Age,'Before','Gender');head(T2,3)
LastName Age Gender ____________ ___ __________ {'Smith' } 38 {'Male' } {'Johnson' } 43 {'Male' } {'Williams'} 38 {'Female'}
Insert more variables after Age
. Since Age is a table variable in T2, specify its name as a character vector.
T3 = addvars(T2,Height,Weight,'After','Age');head(T3,3)
LastName Age Height Weight Gender ____________ ___ ______ ______ __________ {'Smith' } 38 71 176 {'Male' } {'Johnson' } 43 69 163 {'Male' } {'Williams'} 38 64 131 {'Female'}
Insert Smoker
after the first table variable. You can specify variables by position in the table instead of by name.
T4 = addvars(T3,Smoker,'After',1);head(T4,3)
LastName Smoker Age Height Weight Gender ____________ ______ ___ ______ ______ __________ {'Smith' } true 38 71 176 {'Male' } {'Johnson' } false 43 69 163 {'Male' } {'Williams'} false 38 64 131 {'Female'}
Rename Variables
Open Live Script
Create a table. Add variables and give them new names in the table.
First, create a table from workspace variables.
load patientsT1 = table(LastName,Age,Gender,Smoker);head(T1,3)
LastName Age Gender Smoker ____________ ___ __________ ______ {'Smith' } 38 {'Male' } true {'Johnson' } 43 {'Male' } false {'Williams'} 38 {'Female'} false
Combine Diastolic
and Systolic
into one matrix with two columns. Name the new table variable BloodPressure
.
T2 = addvars(T1,[Diastolic Systolic],'NewVariableNames','BloodPressure');head(T2,3)
LastName Age Gender Smoker BloodPressure ____________ ___ __________ ______ _____________ {'Smith' } 38 {'Male' } true 93 124 {'Johnson' } 43 {'Male' } false 77 109 {'Williams'} 38 {'Female'} false 83 125
Add Height
and Weight
as new table variables. Rename them Inches
and Pounds
.
T3 = addvars(T2,Height,Weight,'Before','Smoker','NewVariableNames',{'Inches','Pounds'});head(T3,3)
LastName Age Gender Inches Pounds Smoker BloodPressure ____________ ___ __________ ______ ______ ______ _____________ {'Smith' } 38 {'Male' } 71 176 true 93 124 {'Johnson' } 43 {'Male' } 69 163 false 77 109 {'Williams'} 38 {'Female'} 64 131 false 83 125
Input Arguments
collapse all
T1
— Input table
table | timetable
Input table, specified as a table or timetable.
var1,...,varN
— Variables to add to output table
arrays, tables, and timetables
Variables to add to the output table, specified as arrays, tables, and timetables. The variables specified by var1,...,varN
all must have the same number of rows as the input table T1
.
Example: T2 = addvars(T1,A)
inserts the workspace variables A
to the right of the last table variable.
Example: T2 = addvars(T1,X,Y,Z)
inserts the workspace variables X
, Y
, and Z
.
location
— Location to insert added variables
character vector | string scalar | integer | logical array
Location to insert added variables, specified as a character vector, string scalar, integer, or logical array.
If
location
is a character vector or string scalar, then it is the name of a variable in the input tableT1
.If
location
is the integern
, then it specifies then
th variable inT1
.If
location
is a logical array, whosen
th element is1
(true
), then it specifies then
th variable inT1
. All other elements oflocation
must be0
(false
).
Example: T2 = addvars(T1,Latitude,'Before','Longitude')
insert the workspace variable Latitude
to the left of the table variable named Longitude
.
Example: T2 = addvars(T1,Y,Z,'After','X')
inserts the workspace variables Y
and Z
to the right of the table variable named X
.
newNames
— Names of added variables
character vector | cell array of character vectors | string array
Names of the added variables, specified as a character vector, cell array of character vectors, or string array.
Example: T2 = addvars(T1,lat,lon,'NewVariableNames',{'Latitude','Longitude'})
inserts the workspace variables lat
and lon
and names the corresponding table variables 'Latitude'
and 'Longitude'
.
Limitations
Use single quotes for the input names
'Before'
,'After'
, and'NewVariableNames'
. To avoid confusion with variable inputs, do not use double-quoted string scalars (such as"Before"
) for these names.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. Formore information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2018a
See Also
mergevars | movevars | removevars | splitvars | renamevars
Topics
- Add, Delete, and Rearrange Table Variables
- Add and Delete Table Rows
- Access Data in Tables
- Rename and Describe Table Variables
- Clean Messy and Missing Data in Tables
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office