This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

addvars

Add variables to table or timetable

Syntax

T2 = addvars(T1,var1,...,varN)
T2 = addvars(T1,var1,...,varN,'Before',location)
T2 = addvars(T1,var1,...,varN,'After',location)
T2 = addvars(___,'NewVariableNames',newNames)

Description

example

T2 = addvars(T1,var1,...,varN) adds the variables specified by var1,…,varN to the right of the last variable of T1. The input arguments var1,…,varN can 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 by location (see diagram). You can specify location as 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 by location.

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

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 patients
T1 = table(LastName,Age,Height,Weight);
head(T1,3)
ans=3×4 table
     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)
ans=3×6 table
     LastName     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 the patients.mat file. Create a table that contains the names and genders of patients. Then display the first three rows.

load patients
T1 = table(LastName,Gender);
head(T1,3)
ans=3×2 table
     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)
ans=3×3 table
     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)
ans=3×5 table
     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)
ans=3×6 table
     LastName     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.

load patients
T1 = table(LastName,Age,Gender,Smoker);
head(T1,3)
ans=3×4 table
     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)
ans=3×5 table
     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)
ans=3×7 table
     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

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

Introduced in R2018a