Main Content

Stack data from input table or timetable into one variable in output table or timetable

converts the input table or timetable, `S`

= stack(`U`

,`vars`

)`U`

, into the stacked table
or timetable, `S`

. The function stacks values from multiple
variables in `U`

into one variable in `S`

. The
input argument `vars`

specifies which variables from
`U`

to stack, either by their names or their positions in
`U`

.

If `U`

has `n`

rows and `vars`

specifies `m`

variables in `U`

, then
`S`

has `m*n`

rows. The
`stack`

function interleaves values from the specified
variables in `U`

to create one variable in `S`

.
For example, if `U`

has 10 rows and you stack three of the
variables from `U`

, then `S`

has 30 rows. In
general, `S`

contains fewer variables, but more rows, than
`U`

.

The output table or timetable, `S`

, contains a new
`categorical`

variable to indicate which variable in
`U`

the stacked data in each row of `S`

comes
from. The `stack`

function replicates data from the variables in
`U`

that are not stacked.

If

`U`

is a table, then you cannot stack row names.If

`U`

is a timetable, then you cannot stack row times.

converts the table, `S`

= stack(`U`

,`vars`

,`Name,Value`

)`U`

, with additional options specified by one
or more `Name,Value`

pair arguments.

For example, you can specify variable names for the new and stacked variables in
`S`

.

**Create Multiple Stacked Variables in Output**

It is possible to stack values from the input into multiple variables in the output. To create multiple stacked variables in

`S`

, use a cell array to specify multiple groups of variables from`U`

. You can use a cell array to contain multiple values for`vars`

, and a cell array of character vectors or string array to contain multiple values for the`'NewDataVariableName'`

name-value pair argument. All groups must contain the same number of variables.For example, if

`U`

contains four variables, then you can create two stacked variables in`S`

, instead of only one, by using this syntax.vars = {[1 2],[3 4]}; S = stack(U,vars)

As a result,

`S`

has a stacked variable with values from the first and second variables of`U`

, and another stacked variable with values from the third and fourth variables of`U`

.To specify new variable names in

`S`

, use`'NewDataVariableName'`

. The number of names you specify must equal the number of groups specified in`vars`

.vars = {[1 2],[3 4]}; S = stack(U,vars,'NewDataVariableName',{'Vars1And2','Vars3And4'})

When you specify

`vars`

as a cell array of character vectors, then`S`

contains one stacked variable. To specify multiple stacked variables while using variable names, use a cell array of cell arrays of character vectors, or a cell array of string arrays.For example, this syntax creates one stacked variable in

`S`

when`U`

is a table with variables named`Var1`

,`Var2`

,`Var3`

, and`Var4`

.S = stack(U,{'Var1','Var2','Var3','Var4'})

This syntax creates two stacked variables in

`S`

.S = stack(U,{{'Var1','Var2'} {'Var3','Var4'}})

`unstack`

| `join`

| Stack Table
Variables | Unstack Table
Variables | Join Tables