addvars
Adds variables (columns) to a table or timetable.
Category: Language Fundamentals > Data Types
Syntax
T2 = addvars(T, var1, ..., varN)
Appends the variables after the last column of T.
T2 = addvars(___, 'Before', loc)
Inserts the variables before the column loc (a name or index).
T2 = addvars(___, 'After', loc)
Inserts the variables after the column loc.
T2 = addvars(___, 'NewVariableNames', names)
Names the new variables.
Description
It is the non-destructive alternative to assigning T.C = ..., and the only one that can put the new column in a chosen position. Each new variable must have as many rows as the table.
Input arguments
T- A
tableortimetable. var1, ..., varN- Arrays with the same number of rows as
T. 'NewVariableNames'- Text or cell array with one name per new variable. Without it, names such as
Var3are generated.
Output arguments
T2- Table with the extra variables;
Tis not modified.
Example
Append and insert a column
T = table([1; 2], [3; 4], 'VariableNames', {'A', 'B'});
T2 = addvars(T, [5; 6], 'NewVariableNames', 'C')
T3 = addvars(T, [5; 6], 'Before', 'B', 'NewVariableNames', 'C')
Output
T2 = 2×3 table
A B C
_ _ _
1 3 5
2 4 6
T3 = 2×3 table
A C B
_ _ _
1 5 3
2 6 4