cell2struct
Converts a cell array into a structure array.
Category: Language Fundamentals > Data Types
Syntax
s = cell2struct(c, fields, dim)
Builds a structure from c, taking field values along dimension dim.
Description
When dim is 1, each column of c supplies the values of one structure and the rows correspond to the fields; with dim 2 it is the other way round. A 2-by-1 cell with dim 1 therefore gives a single structure of two fields.
The inverse is struct2cell. A mismatch between the number of field names and the length of dim is an error.
Input arguments
c- Cell array of field values.
fields- Field names: a cell array of character vectors, a char matrix, or a string array.
dim- Dimension of
cthat runs through the fields. Its length must equal the number of field names.
Output arguments
s- Structure array. The other dimension of
cbecomes the structure array dimension.
Examples
One record
s = cell2struct({'Bob'; 42}, {'name'; 'age'}, 1)
Output
s = struct with fields:
name: 'Bob'
age: 42
Several records
t = cell2struct({1 2; 3 4}, {'a','b'}, 2);
size(t)
t(2)
Output
ans = 1×2
2 1
ans = struct with fields:
a: 3
b: 4