Stack Table Variables
Combine values from multiple table variables into one table variable in the Live Editor
Since R2020a
Description
The Stack Table Variables task lets you interactively stack values from multiple table or timetable variables into one variable. The task automatically generates MATLAB® code for your live script.
More
Related Functions
Stack Table Variables generates code that uses the
stack
function.
Open the Task
To add the Stack Table Variables task to a live script in the MATLAB Editor:
On the Live Editor tab, select Task > Stack Table Variables.
In a code block in the script, type a relevant keyword, such as
stack
. Select Stack Table Variables from the suggested command completions.
Examples
Stack Table Variables Using Live Editor Task
This example shows how to stack variables by using the Stack Table Variables Live Editor task. It takes values from multiple variables in an input table, and combines them into one variable in an output table.
First, load a table that contains estimated influenza rates along the East Coast of the United States. The table has three variables with rates for the Northeast (NE
), Mid-Atlantic (MidAtl
), and Southeast (SE
) regions. (These data come from the Google Flu Trends project, since discontinued.)
load fluByRegion
fluByRegion
fluByRegion=6×4 table
Month NE MidAtl SE
_____________ ______ ______ ______
October-2005 1.1902 1.1865 1.273
November-2005 1.361 1.412 1.582
December-2005 1.5003 1.6043 1.8625
January-2006 1.7772 1.883 1.954
February-2006 2.135 2.1227 2.4803
March-2006 2.2345 1.992 2.0203
Stack the flu rate data so that the flu rates from the NE
, MidAtl
, and SE
variables are in one variable in the output table.
Add the Stack Table Variables task to your live script.
To copy values from the
Month
variable to rows of the output table, selectConstant
from the drop-down list above it in the task.To combine the values from the
NE
,MidAtl
, andSE
variables by stacking them into one variable of the output, selectStack
for each variable.
The name of the stacked variable in the output comes from combining the input variable names.
The task also stacks the names of the input variables into a new variable, named NE_MidAtl_SE_Indicator
. In this example, the names of regions are the names of variables in the input table. Those names become data values in a new variable that indicates the regions for the flu rate data.
stackedTable=18×3 table
Month NE_MidAtl_SE_Indicator NE_MidAtl_SE
_____________ ______________________ ____________
October-2005 NE 1.1902
October-2005 MidAtl 1.1865
October-2005 SE 1.273
November-2005 NE 1.361
November-2005 MidAtl 1.412
November-2005 SE 1.582
December-2005 NE 1.5003
December-2005 MidAtl 1.6043
December-2005 SE 1.8625
January-2006 NE 1.7772
January-2006 MidAtl 1.883
January-2006 SE 1.954
February-2006 NE 2.135
February-2006 MidAtl 2.1227
February-2006 SE 2.4803
March-2006 NE 2.2345
⋮
Give the variables of the output table more meaningful names. To rename table variables, use the renamevars
function.
fluRate = renamevars(stackedTable, ... ["NE_MidAtl_SE_Indicator","NE_MidAtl_SE"], ... ["Region","Flu Rate"])
fluRate=18×3 table
Month Region Flu Rate
_____________ ______ ________
October-2005 NE 1.1902
October-2005 MidAtl 1.1865
October-2005 SE 1.273
November-2005 NE 1.361
November-2005 MidAtl 1.412
November-2005 SE 1.582
December-2005 NE 1.5003
December-2005 MidAtl 1.6043
December-2005 SE 1.8625
January-2006 NE 1.7772
January-2006 MidAtl 1.883
January-2006 SE 1.954
February-2006 NE 2.135
February-2006 MidAtl 2.1227
February-2006 SE 2.4803
March-2006 NE 2.2345
⋮
Related Examples
Parameters
Input table
— Names of table or timetable
drop-down list items
Specify the name of the input table or timetable timetables from the lists of all the nonempty tables and timetables that are in the workspace.
Variables to stack
— Actions for input variables
drop-down list items
Specify the action to take for each variable in the input table or timetable. When stacking variables, there are three possible actions:
Constant — Copy the variable to the output table or timetable. Replicate its values where needed in rows of the output.
Stack — Stack the values from the variable into one variable of the output. For example, if you choose to stack three variables, all their values go into one variable of the output.
Discard — Exclude the variable and its values from the output.
Version History
Introduced in R2020aR2022a: Live Editor task does not run automatically if inputs have more than 1 million elements
This Live Editor task does not run automatically if the inputs have more than 1 million elements. In previous releases, the task always ran automatically for inputs of any size. If the inputs have a large number of elements, then the code generated by this task can take a noticeable amount of time to run (more than a few seconds).
When a task does not run automatically, the Autorun indicator is disabled. You can either run the task manually when needed or choose to enable the task to run automatically.
See Also
Functions
Live Editor Tasks
Apps
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)