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.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Stack data from multiple variables into single variable

`S = stack(U,vars)`

`S = stack(U,vars,Name,Value)`

```
[S,iu] =
stack(___)
```

converts
the table or timetable, `S`

= stack(`U`

,`vars`

)`U`

, into an equivalent table
or timetable, `S`

, that is stacked. The `stack`

function
stacks up multiple variables from `U`

, specified
by `vars`

, into a single variable in `S`

.
In general, `S`

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

.

The output argument, `S`

, contains a new categorical
variable to indicate which variable in `U`

the stacked
data in each row came from. `stack`

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

.

You can specify more than one group of data variables in

`U`

, and each group becomes a stacked data variable in`S`

. Use a cell array to contain multiple values for`vars`

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

name-value pair argument. All groups must contain the same number of variables.

Was this topic helpful?