table2struct

Converts a table to a structure array.

Category: Language Fundamentals > Data Types

Syntax

s = table2struct(T)

Returns a column struct array with one element per row and one field per variable.

Description

Each row of the table becomes a struct element, so s(2) is the second record. This is a handy form for looping over records or for encoding a table as JSON with jsonencode.

The inverse is struct2table.

Input arguments

T
Table.

Output arguments

s
height(T)-by-1 struct array whose fields have the names of the variables.

Example

One struct per row

T = table([1;2], {'a';'b'}, 'VariableNames', {'n','s'});
s = table2struct(T);
size(s)
s(2)
Output
ans = 1×2

     2     1
ans = struct with fields:

    n: 2
    s: 'b'

See also

struct2table table2cell jsonencode table