Main Content


(Not Recommended) Stack dataset array from multiple variables into single variable

The dataset data type is not recommended. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.


B = stack(A,datavars)
[B,iA] = stack(A,datavars)
B = stack(A,datavars,Parameter,value)


B = stack(A,datavars) stacks multiple variables in dataset array A into a single variable in B. In general, B contains fewer variables but more observations than A.

datavars specifies a group of m data variables in A. stack creates a single data variable in B by interleaving their values, and if A has n observations, then B has m-by-n observations. In other words, stack takes the m data values from each observation in A and stacks them up to create m observations in B. datavars is a positive integer, a vector of positive integers, a character vector, a string array, a cell array of character vectors, or a logical vector. stack also creates a grouping variable in B to indicate which of the m data variables in A each observation in B corresponds to.

stack assigns values for the "per-variable properties (e.g., Units and VarDescription) for the new data variable in B from the corresponding property values for the first variable listed in datavars.

stack copies the remaining variables from A to B without stacking, by replicating each of their values m times. These variables are typically grouping variables. Because their values are constant across each group of m observations in B, they identify which observation in A an observation in B came from.

[B,iA] = stack(A,datavars) returns an index vector iA indicating the correspondence between observations in B and those in A. stack creates B(j,:) using A(iA(j),datavarss).

For more information on grouping variables, see Grouping Variables.

Input Arguments

B = stack(A,datavars,Parameter,value) uses the following parameter name/value pairs to control how stack converts variables in A to variables in B:

'ConstVars'Variables in A to copy to B without stacking. ConstVars is a positive integer, a vector of positive integers, a character vector, a string array, a cell array of character vectors, or a logical vector. The default is all variables in A not specified in datavars.
'NewDataVarName'A name for the data variable to be created in B. The default is a concatenation of the names of the m variables that are stacked up.
'IndVarName'A name for the grouping variable to create in B to indicate the source of each value in the new data variable. The default is based on the 'NewDataVarName' parameter.

You can also specify multiple groups of data variables in A, each of which becomes a variable in B. All groups must contain the same number of variables. Use a string array or cell array of character vectors to contain multiple parameter values for datavars or to contain multiple values for 'NewDataVarName'.


Combine several variables for estimated influenza rates into a single variable. Then unstack the estimated influenza rates by date.

load flu
% FLU has a 'Date' variable, and 10 variables for estimated influenza rates
% (in 9 different regions, estimated from Google searches, plus a
% nationwide estimate from the CDC). Combine those 10 variables into an
% array that has a single data variable, 'FluRate', and an indicator
% variable, 'Region', that says which region each estimate is from.
[flu2,iflu] = stack(flu, 2:11, 'NewDataVarName','FluRate', ...
% The second observation in FLU is for 10/16/2005.  Find the observations
% in FLU2 that correspond to that date.
% Use the 'Date' variable from that array to split 'FluRate' into 52
% separate variables, each containing the estimated influenza rates for
% each unique date.  The new array has one observation for each region.  In
% effect, this is the original array FLU "on its side".
dateNames = cellstr(datestr(flu.Date,'mmm_DD_YYYY'));
[flu3,iflu2] = unstack(flu2, 'FluRate', 'Date', ...
% Since observations in FLU3 represent regions, IFLU2 indicates the first
% occurrence in FLU2 of each region.