Main Content

addvars

Add variables to table or timetable

Description

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.

example

Examples

collapse all

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 ages, heights, and weights of patients.

load patients
T1 = table(Age,Height,Weight)
T1=100×3 table
    Age    Height    Weight
    ___    ______    ______

    38       71       176  
    43       69       163  
    38       64       131  
    40       67       133  
    49       64       119  
    46       68       142  
    33       64       142  
    40       68       180  
    28       68       183  
    31       66       132  
    45       68       128  
    42       66       137  
    25       71       174  
    39       72       202  
    36       65       129  
    48       71       181  
      ⋮

Add the workspace variables Smoker and LastName to the table. Before adding LastName, convert it to a string array.

LastName = string(LastName);
T2 = addvars(T1,Smoker,LastName)
T2=100×5 table
    Age    Height    Weight    Smoker     LastName 
    ___    ______    ______    ______    __________

    38       71       176      true      "Smith"   
    43       69       163      false     "Johnson" 
    38       64       131      false     "Williams"
    40       67       133      false     "Jones"   
    49       64       119      false     "Brown"   
    46       68       142      false     "Davis"   
    33       64       142      true      "Miller"  
    40       68       180      false     "Wilson"  
    28       68       183      false     "Moore"   
    31       66       132      false     "Taylor"  
    45       68       128      false     "Anderson"
    42       66       137      false     "Thomas"  
    25       71       174      false     "Jackson" 
    39       72       202      true      "White"   
    36       65       129      false     "Harris"  
    48       71       181      true      "Martin"  
      ⋮

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 locations of patients.

load patients
LastName = string(LastName);
Location = string(Location);
T1 = table(LastName,Location)
T1=100×2 table
     LastName              Location          
    __________    ___________________________

    "Smith"       "County General Hospital"  
    "Johnson"     "VA Hospital"              
    "Williams"    "St. Mary's Medical Center"
    "Jones"       "VA Hospital"              
    "Brown"       "County General Hospital"  
    "Davis"       "St. Mary's Medical Center"
    "Miller"      "VA Hospital"              
    "Wilson"      "VA Hospital"              
    "Moore"       "St. Mary's Medical Center"
    "Taylor"      "County General Hospital"  
    "Anderson"    "County General Hospital"  
    "Thomas"      "St. Mary's Medical Center"
    "Jackson"     "VA Hospital"              
    "White"       "VA Hospital"              
    "Harris"      "St. Mary's Medical Center"
    "Martin"      "VA Hospital"              
      ⋮

Insert the workspace variable, Age, before the table variable, Location. To refer to a table variable by name, specify its name as a string.

T2 = addvars(T1,Age,Before="Location")
T2=100×3 table
     LastName     Age             Location          
    __________    ___    ___________________________

    "Smith"       38     "County General Hospital"  
    "Johnson"     43     "VA Hospital"              
    "Williams"    38     "St. Mary's Medical Center"
    "Jones"       40     "VA Hospital"              
    "Brown"       49     "County General Hospital"  
    "Davis"       46     "St. Mary's Medical Center"
    "Miller"      33     "VA Hospital"              
    "Wilson"      40     "VA Hospital"              
    "Moore"       28     "St. Mary's Medical Center"
    "Taylor"      31     "County General Hospital"  
    "Anderson"    45     "County General Hospital"  
    "Thomas"      42     "St. Mary's Medical Center"
    "Jackson"     25     "VA Hospital"              
    "White"       39     "VA Hospital"              
    "Harris"      36     "St. Mary's Medical Center"
    "Martin"      48     "VA Hospital"              
      ⋮

Insert more variables after Age. Since Age is a table variable in T2, specify its name as a string.

T3 = addvars(T2,Height,Weight,After="Age")
T3=100×5 table
     LastName     Age    Height    Weight             Location          
    __________    ___    ______    ______    ___________________________

    "Smith"       38       71       176      "County General Hospital"  
    "Johnson"     43       69       163      "VA Hospital"              
    "Williams"    38       64       131      "St. Mary's Medical Center"
    "Jones"       40       67       133      "VA Hospital"              
    "Brown"       49       64       119      "County General Hospital"  
    "Davis"       46       68       142      "St. Mary's Medical Center"
    "Miller"      33       64       142      "VA Hospital"              
    "Wilson"      40       68       180      "VA Hospital"              
    "Moore"       28       68       183      "St. Mary's Medical Center"
    "Taylor"      31       66       132      "County General Hospital"  
    "Anderson"    45       68       128      "County General Hospital"  
    "Thomas"      42       66       137      "St. Mary's Medical Center"
    "Jackson"     25       71       174      "VA Hospital"              
    "White"       39       72       202      "VA Hospital"              
    "Harris"      36       65       129      "St. Mary's Medical Center"
    "Martin"      48       71       181      "VA Hospital"              
      ⋮

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)
T4=100×6 table
     LastName     Smoker    Age    Height    Weight             Location          
    __________    ______    ___    ______    ______    ___________________________

    "Smith"       true      38       71       176      "County General Hospital"  
    "Johnson"     false     43       69       163      "VA Hospital"              
    "Williams"    false     38       64       131      "St. Mary's Medical Center"
    "Jones"       false     40       67       133      "VA Hospital"              
    "Brown"       false     49       64       119      "County General Hospital"  
    "Davis"       false     46       68       142      "St. Mary's Medical Center"
    "Miller"      true      33       64       142      "VA Hospital"              
    "Wilson"      false     40       68       180      "VA Hospital"              
    "Moore"       false     28       68       183      "St. Mary's Medical Center"
    "Taylor"      false     31       66       132      "County General Hospital"  
    "Anderson"    false     45       68       128      "County General Hospital"  
    "Thomas"      false     42       66       137      "St. Mary's Medical Center"
    "Jackson"     false     25       71       174      "VA Hospital"              
    "White"       true      39       72       202      "VA Hospital"              
    "Harris"      false     36       65       129      "St. Mary's Medical Center"
    "Martin"      true      48       71       181      "VA Hospital"              
      ⋮

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

First, create a table from workspace variables.

load patients
LastName = string(LastName);
T1 = table(LastName,Age,Smoker)
T1=100×3 table
     LastName     Age    Smoker
    __________    ___    ______

    "Smith"       38     true  
    "Johnson"     43     false 
    "Williams"    38     false 
    "Jones"       40     false 
    "Brown"       49     false 
    "Davis"       46     false 
    "Miller"      33     true  
    "Wilson"      40     false 
    "Moore"       28     false 
    "Taylor"      31     false 
    "Anderson"    45     false 
    "Thomas"      42     false 
    "Jackson"     25     false 
    "White"       39     true  
    "Harris"      36     false 
    "Martin"      48     true  
      ⋮

Combine Diastolic and Systolic into one matrix with two columns. Name the new table variable BloodPressure.

T2 = addvars(T1,[Diastolic Systolic],NewVariableNames="BloodPressure")
T2=100×4 table
     LastName     Age    Smoker    BloodPressure
    __________    ___    ______    _____________

    "Smith"       38     true        93    124  
    "Johnson"     43     false       77    109  
    "Williams"    38     false       83    125  
    "Jones"       40     false       75    117  
    "Brown"       49     false       80    122  
    "Davis"       46     false       70    121  
    "Miller"      33     true        88    130  
    "Wilson"      40     false       82    115  
    "Moore"       28     false       78    115  
    "Taylor"      31     false       86    118  
    "Anderson"    45     false       77    114  
    "Thomas"      42     false       68    115  
    "Jackson"     25     false       74    127  
    "White"       39     true        95    130  
    "Harris"      36     false       79    114  
    "Martin"      48     true        92    130  
      ⋮

Add Height and Weight as new table variables. Rename them Inches and Pounds.

T3 = addvars(T2,Height,Weight,Before="Smoker",NewVariableNames=["Inches" "Pounds"])
T3=100×6 table
     LastName     Age    Inches    Pounds    Smoker    BloodPressure
    __________    ___    ______    ______    ______    _____________

    "Smith"       38       71       176      true        93    124  
    "Johnson"     43       69       163      false       77    109  
    "Williams"    38       64       131      false       83    125  
    "Jones"       40       67       133      false       75    117  
    "Brown"       49       64       119      false       80    122  
    "Davis"       46       68       142      false       70    121  
    "Miller"      33       64       142      true        88    130  
    "Wilson"      40       68       180      false       82    115  
    "Moore"       28       68       183      false       78    115  
    "Taylor"      31       66       132      false       86    118  
    "Anderson"    45       68       128      false       77    114  
    "Thomas"      42       66       137      false       68    115  
    "Jackson"     25       71       174      false       74    127  
    "White"       39       72       202      true        95    130  
    "Harris"      36       65       129      false       79    114  
    "Martin"      48       71       181      true        92    130  
      ⋮

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 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 to insert added variables, specified as a string scalar, character vector, integer, or logical array.

  • If location is a string scalar or character vector, then it is the name of a variable in the input table T1.

  • If location is the integer n, then it specifies the nth variable in T1.

  • If location is a logical array, whose nth element is 1 (true), then it specifies the nth variable in T1. All other elements of location must be 0 (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.

Names of the added variables, specified as a string array, character vector, or cell array of character vectors.

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.

Output Arguments

collapse all

Output table with added variables, returned as a table or timetable.

Limitations

  • Name=Value syntax is recommended for name-value arguments. But if you use Name,Value syntax, 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

expand all

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

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2018a