struct2table - Convert structure array to table - MATLAB (original) (raw)

Convert structure array to table

Syntax

Description

[T](#btx32%5Fl-1%5Fsep%5Fshared-T) = struct2table([S](#btx32%5Fl-1-S)) converts a structure array to a table. Each field of the input structure becomes a variable in the output table.

example

[T](#btx32%5Fl-1%5Fsep%5Fshared-T) = struct2table([S](#btx32%5Fl-1-S),[Name,Value](#namevaluepairarguments)) creates a table from a structure array, with additional options specified by one or more Name,Value arguments.

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

example

Examples

collapse all

Convert Scalar Structure to Table

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=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

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=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

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

S — Input array

structure

Input array, specified as a structure.

Name-Value Arguments

Specify optional pairs of arguments asName1=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.

RowNames — Row names

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

Row names, specified as a cell array of character vectors or string array, whose elements are nonempty and distinct. The number of row names must equal the number of rows of the input array.

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

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

DimensionNames — Dimension 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. However, a dimension name cannot match any table variable name or the reserved names'Properties', 'RowNames','VariableNames', or ':'.

As an alternative, in all releases you can specify dimension names by setting theDimensionNames property of the table.

AsArray — Flag to treat input as structure array

true or 1 | false or 0

Flag to treat the input as a structure array, specified as a numeric or logical 1 (true) or0 (false).

trueDefault if input is nonscalar structure array Converts structure array to a table with one row for each structure and one variable for each field of the structure array. The fields can have different sizes.If the values in fieldS(1:m).fieldname have: Compatible sizes and data types, and each value has one row, then the corresponding table variable is a homogeneous array.**Example:**S(1).a = [1 2] S(2).a = [3 4] S(1).b = 5 S(2).b = 6 T = struct2table(S) T = 2×2 table a b ______ _ 1 2 5 3 4 6 Different sizes, incompatible data types, or any value has more than one row, then the corresponding table variable is a cell array.**Example:**S(1).a = [1 2] S(2).a = [3 4 5 6] S(1).b = 7 S(2).b = 8 T = struct2table(S) T = 2×2 table a b ___________ _ {[ 1 2]} 7 {[3 4 5 6]} 8
falseDefault if input is scalar structure Converts a scalar structure withn fields into anm-by-n table. Each field must have m rows.**Example:**S.a = [1;2;3] S.b = [4 5;6 7;8 9] T = struct2table(S) T = 3×2 table a b _ ______ 1 4 5 2 6 7 3 8 9

Output Arguments

collapse all

Output table, returned as a table. The table can store metadata such as descriptions, variable units, variable names, and row names. For more information, see the Properties section of table.

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

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

Version History

Introduced in R2013b