Convert structure array to table (2024)

Convert structure array to table

collapse all in page

Syntax

T = struct2table(S)

T = struct2table(S,Name,Value)

Description

example

T = struct2table(S) convertsthe structure array, S, to a table, T.Each field of S becomes a variable in T.

example

T = struct2table(S,Name,Value) createsa table from a structure array, S, with additionaloptions specified by one or more Name,Value pairarguments.

For example, you can specify row names to include in the table.

Examples

collapse all

Convert Scalar Structure to Table

Open Live Script

Convert a scalar structure to a table using the default options.

Create a structure array, S.

S.Name = ["Chang";"Brown";"Ruiz"];S.Smoker = ["Y";"N";"Y"];S.SystolicBP = [124;122;130];S.DiastolicBP = [93;80;92];

The scalar structure, S, has four fields, each with three rows.

Convert the structure array to a table.

T = struct2table(S)
T=3×4 table Name Smoker SystolicBP DiastolicBP _______ ______ __________ ___________ "Chang" "Y" 124 93 "Brown" "N" 122 80 "Ruiz" "Y" 130 92 

The structure field names in S become the variable names in the output table. The size of T is 3-by-4.

Change Name from a variable to row names by modifying the table property, T.Properties.RowNames, and then deleting the variable Name.

T.Properties.RowNames = T.Name;T.Name = [];T
T=3×3 table Smoker SystolicBP DiastolicBP ______ __________ ___________ Chang "Y" 124 93 Brown "N" 122 80 Ruiz "Y" 130 92 

Convert Nonscalar Structure Array to Table

Open Live Script

Create a nonscalar structure array, S.

S(1,1).Name = "Chang";S(1,1).Smoker = "Y";S(1,1).SystolicBP = 124;S(1,1).DiastolicBP = 93;S(2,1).Name = "Brown";S(2,1).Smoker = "N";S(2,1).SystolicBP = 122;S(2,1).DiastolicBP = 80;S(3,1).Name = "Ruiz";S(3,1).Smoker = "Y";S(3,1).SystolicBP = 130;S(3,1).DiastolicBP = 92;S
S=3×1 struct array with fields: Name Smoker SystolicBP DiastolicBP

S is a 3-by-1 structure array with four fields.

Convert the structure array to a table.

T = struct2table(S)
T=3×4 table Name Smoker SystolicBP DiastolicBP _______ ______ __________ ___________ "Chang" "Y" 124 93 "Brown" "N" 122 80 "Ruiz" "Y" 130 92 

The structure field names in S become the variable names in the output table. The size of T is 3-by-4.

Treat Scalar Structure As Array

Open Live Script

Use "AsArray",true to create a table from a scalar structure whose fields have different numbers of rows.

Create a scalar structure, S, with fields name, billing, and test.

S.name = "John Doe";S.billing = 127.00;S.test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205]
S = struct with fields: name: "John Doe" billing: 127 test: [3x3 double]

The fields have different numbers of rows. Therefore, you cannot use struct2table(S), which uses "AsArray",false by default.

Treat the scalar structure as an array and convert it to a table.

T = struct2table(S,"AsArray",true)
T=1×3 table name billing test __________ _______ ____________ "John Doe" 127 {3x3 double}

T contains one row.

Input Arguments

collapse all

SStructure array
structure array

Structure array, specified as a scalar structure array.

  • If S is a scalar structure with n fields,all of which have m rows, then T isan m-by-n table.

  • If S is a nonscalar m-by-1 structurearray with n fields, then T isan m-by-n table.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: "RowNames",["row1","row2","row3"] uses the row names, row1, row2, and row3 for the table, T.

RowNamesRow names
{} (default) | string array | cell array of character vectors

Row names, specified as the comma-separated pair consisting of "RowNames" and a string array or a cell array of character vectors whose elements are nonempty and distinct.

Row names can have any Unicode® characters, including spaces and non-ASCII characters.

If you specify row names that have leading or trailing whitespace characters, then struct2table removes them from the row names.

DimensionNamesDimension names
two-element cell array of character vectors | two-element string array

Since R2021a

Dimension names, specified as a two-element cell array of character vectors or two-element string array whose elements are nonempty and distinct.

Dimension names can have any Unicode characters, including spaces and non-ASCII characters.

Before R2021a, you can specify dimension names only by setting the DimensionNames property of the output.

AsArrayIndicator for how to treat scalar structure
false (default) | true | 0 | 1

Indicator for how to treat scalar structure, specified as the comma-separated pair consisting of "AsArray" and either false, true, 0, or 1.

true

struct2table converts S to a table with one row and n variables. The variables can be different sizes.

false

struct2table converts a scalar structure array with n fields into an m-by-n table. Each field must have m rows. This is the default behavior

Output Arguments

collapse all

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2013b

See Also

table2struct | cell2table | array2table | table

Topics

  • Access Data in Tables

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.

Convert structure array to table (1)

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:

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
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Convert structure array to table (2024)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6271

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.