Pivot Table - Summarize tabular data in pivoted table in the Live Editor - MATLAB (original) (raw)

Main Content

Summarize tabular data in pivoted table in the Live Editor

Since R2023b

Description

The Pivot Table task lets you interactively summarize tabular data in a pivoted table according to column and row groups. The task automatically generates MATLAB® code for your live script. For more information about Live Editor tasks generally, see Add Interactive Tasks to a Live Script.

A pivoted table provides a summary of tabular data. Pivoted tables are useful for analyzing and providing insights into large data sets and organizing data from another perspective, according to column and row groups. The column and row labels of a pivoted table are group names, and the data values are group counts or the result of another computation method. For more information, see Pivoting Operation or watch How to Create Pivot Tables in MATLAB (4 min, 11 sec).

.

Using this task, you can:

Pivot Table generates code that uses the pivot function.

Pivot Table task in the Live Editor

Open the Task

To add the Pivot Table task to a live script in the MATLAB Editor:

Examples

expand all

Interactively compute the group counts for table data for groups defined by a logical grouping variable and a discretized numeric grouping variable using the Pivot Table Live Editor task.

Create a table from a file that contains information about 100 hospital patients.

T = readtable("patients.xls");

Open the Pivot Table task in the Live Editor. To understand the relationship between two variables, specify grouping variables to designate the pivoted table rows and variables. Select the Smoker variable in the Rows panel, and select the Age variable in the Columns panel.

By default, the Pivot Table task fills the data values of the pivoted table with the number of members in each group. The Values panel shows that group counts fill the pivoted table.

There are 25 unique values in the Age variable, which makes the default pivoted table difficult to interpret. Reduce the number of variables in the pivoted table by creating 5 evenly spaced bins for the Age grouping variable. Apply a binning method to Age by clicking the button and specifying the number of bins as 5.

The resulting pivoted table contains the number of elements in each age range for smoking and nonsmoking patients. Visualize the counts in a grouped bar graph using the Chart field.

Interactively select a subset of tabular data and summarize the filtered data in a pivoted table using the Pivot Table Live Editor task.

Create a table from a file that contains information about 100 hospital patients.

T = readtable("patients.xls");

Open the Pivot Table task in the Live Editor. Expand the Filter rows section, and click the triangle icon in the header of the Smoker variable. Apply a filter to focus only on rows in the input table that correspond to nonsmoking patients. The resulting table is filtered from 100 to 66 rows.

Then, specify the grouping variables. Select the Location variable in the Rows panel, and select the SelfAssessedHealthStatus variable in the Columns panel.

By default, the Pivot Table task fills the data values of the pivoted table with the number of members in each group. Instead, fill the pivoted table with the average age of each group. Select the Age variable and the Mean computation method in the Values panel.

The resulting pivoted table contains the average age of nonsmoking patients reporting each health status for each hospital location. To include the average age for each health status regardless of the location, select Include totals for columns in the Select optional pivot parameters section.

Parameters

expand all

Select the name of the input table or timetable from the list of all the nonempty tables and timetables that are in the workspace.

Apply a filter to focus only on rows in the input table that satisfy the filtering condition. Expand the Filter rows section of the task, click the triangle icon in the header of a table variable, and use the options to interactively select a subset of the data.

For example, pivot only rows corresponding to patients who are at least 29 years old.

Filtering figure for numeric Age variable

Click the Add button. Then, select a table or timetable variable.

Select the data values for the pivoted table. Choose<group> followed by Count orPercentage, or choose a data variable from the input table to rearrange or aggregate. For aggregation, select from the provided statistics or select Custom function to specify a function handle or write a local function.

Tips

Version History

Introduced in R2023b

expand all

You can rearrange data that does not require aggregation, including nonnumeric data, by selecting the No aggregation computation method in theValues pane of the Select pivot variables section.