mergevars

Combines several table variables into a single multi-column variable.

Category: Language Fundamentals > Data Types

Syntax

T2 = mergevars(T, vars)

Merges the variables vars of the table T into one variable.

T2 = mergevars(T, vars, 'NewVariableName', name)

Names the merged variable.

Description

The merged variable is a matrix with one column per original variable, so the variables must have the same number of rows and compatible types. It is the opposite of splitting a matrix variable with splitvars, and it is handy for treating coordinates or measurements as a single unit.

Input arguments

T
A table or timetable.
vars
Variables to merge: names, positions or a logical mask.
NewVariableName
Name for the new variable. By default a name is built from the originals.

Output arguments

T2
A table in which the chosen variables are replaced by one, placed where the first of them was.

Example

Two variables into one

T = table([1;2], [3;4], [5;6], 'VariableNames', {'a','b','c'});
M = mergevars(T, {'a','b'}, 'NewVariableName', 'ab')
size(M.ab)
Output
M = 2×2 table

      ab      c
    ______    _

    1    3    5
    2    4    6
ans = 1×2

     2     2

See also

splitvars movevars table removevars addvars