struct2table

Converts a struct array into a table.

Category: Language Fundamentals > Data Types

Syntax

T = struct2table(S)

Turns each element of a struct array into a table row and each field into a variable.

Description

The names of the fields become the names of the variables. Numeric fields turn into numeric columns and text fields into cell columns of character vectors. Use table2struct to go back.

This is a convenient way to bring records, for example read from JSON with jsondecode, into a table that can be sorted, filtered and summarized.

Input arguments

S
Struct array whose elements have the same fields.

Output arguments

T
Table with one row per element of S and one variable per field.

Example

From a struct array

s = struct('a', {1; 2}, 'b', {'x'; 'y'});
T = struct2table(s)
Output
T = 2×2 table

    a      b  
    _    _____

    1    {'x'}
    2    {'y'}

Notes

A scalar struct whose fields are column vectors becomes a single-row table with cell contents in MatLivre, whereas MATLAB spreads the vectors over the rows. Pass a struct array to get one row per record.

See also

table2struct struct2cell cell2table table jsondecode