Main Content

addvars

Add variables to table or timetable

Description

example

T2 = addvars(T1,var1,...,varN)adds the variables specified byvar1,…,varNto the right of the last variable ofT1. The input argumentsvar1,…,varNcan include arrays of any type, tables, and timetables. All input arguments must have the same number of rows.

example

T2 = addvars(T1,var1,...,varN,'Before',location)inserts the variables to the left of the table variable indicated bylocation(see diagram). You can specifylocationas a variable name, or a numeric or logical index.

example

T2 = addvars(T1,var1,...,varN,'After',location)inserts the variables to the right of the table variable indicated bylocation.

example

T2 = addvars(___,'NewVariableNames',newNames)renames the added variables inT2using the names specified bynewNames. The number of names innewNamesmust 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

Create a table. Then add variables from the workspace to the table.

Load arrays from thepatients.matfile. Create a table that contains the names, ages, heights, and weights of patients. Then display the first three rows.

loadpatientsT1 = table(LastName,Age,Height,Weight); head(T1,3)
ans=3×4 tableLastName Age Height Weight ____________ ___ ______ ______ {'Smith' } 38 71 176 {'Johnson' } 43 69 163 {'Williams'} 38 64 131

Add the workspace variables,GenderandSmoker, to the table.

T2 = addvars(T1,Gender,Smoker); head(T2,3)
ans=3×6 tableLastName Age Height Weight Gender Smoker ____________ ___ ______ ______ __________ ______ {'Smith' } 38 71 176 {'Male' } true {'Johnson' } 43 69 163 {'Male' } false {'Williams'} 38 64 131 {'Female'} false

Create a table. Then insert variables before and after specified locations in the table.

Load arrays from thepatients.matfile. Create a table that contains the names and genders of patients. Then display the first three rows.

loadpatientsT1 = table(LastName,Gender); head(T1,3)
ans=3×2 tableLastName 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)
ans=3×3 tableLastName Age Gender ____________ ___ __________ {'Smith' } 38 {'Male' } {'Johnson' } 43 {'Male' } {'Williams'} 38 {'Female'}

Insert more variables afterAge. 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)
ans=3×5 tableLastName Age Height Weight Gender ____________ ___ ______ ______ __________ {'Smith' } 38 71 176 {'Male' } {'Johnson' } 43 69 163 {'Male' } {'Williams'} 38 64 131 {'Female'}

InsertSmokerafter 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)
ans=3×6 tableLastName Smoker Age Height Weight Gender ____________ ______ ___ ______ ______ __________ {'Smith' } true 38 71 176 {'Male' } {'Johnson' } false 43 69 163 {'Male' } {'Williams'} false 38 64 131 {'Female'}

Create a table. Add variables and give them new names in the table.

First, create a table from workspace variables.

loadpatientsT1 = table(LastName,Age,Gender,Smoker); head(T1,3)
ans=3×4 tableLastName Age Gender Smoker ____________ ___ __________ ______ {'Smith' } 38 {'Male' } true {'Johnson' } 43 {'Male' } false {'Williams'} 38 {'Female'} false

CombineDiastolicandSystolicinto one matrix with two columns. Name the new table variable血压.

T2 = addvars(T1,[Diastolic Systolic],'NewVariableNames','BloodPressure'); head(T2,3)
ans=3×5 tableLastName Age Gender Smoker BloodPressure ____________ ___ __________ ______ _____________ {'Smith' } 38 {'Male' } true 93 124 {'Johnson' } 43 {'Male' } false 77 109 {'Williams'} 38 {'Female'} false 83 125

AddHeightandWeightas new table variables. Rename themInchesandPounds.

T3 = addvars(T2,Height,Weight,'Before','Smoker','NewVariableNames',{'Inches','Pounds'}); head(T3,3)
ans=3×7 table吸烟者BloodPres LastName年龄性别英寸磅sure ____________ ___ __________ ______ ______ ______ _____________ {'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

Input table, specified as a table or timetable.

Variables to add to the output table, specified as arrays, tables, and timetables. The variables specified byvar1,...,varNall must have the same number of rows as the input tableT1.

Example:T2 = addvars(T1,A)inserts the workspace variablesAto the right of the last table variable.

Example:T2 = addvars(T1,X,Y,Z)inserts the workspace variablesX,Y, andZ.

Location to insert added variables, specified as a character vector, string scalar, integer, or logical array.

  • Iflocationis a character vector or string scalar, then it is the name of a variable in the input tableT1.

  • Iflocationis the integern, then it specifies thenth variable inT1.

  • Iflocationis a logical array, whosenth element is1(true), then it specifies thenth variable inT1. All other elements oflocationmust be0(false).

Example:T2 = addvars(T1,Latitude,'Before','Longitude')insert the workspace variableLatitudeto the left of the table variable namedLongitude.

Example:T2 = addvars(T1,Y,Z,'After','X')inserts the workspace variablesYandZto the right of the table variable namedX.

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 variableslatandlonand 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

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2018a