struct
Creates a structure array with named fields.
Category: Language Fundamentals > Data Types
Syntax
s = struct
Creates a scalar struct with no fields.
s = struct(field1, value1, ...)
Creates a struct with the given fields and values.
s = struct(field, {})
Creates an empty struct array that has the field.
Description
A struct groups related values under names, accessed as s.name. Fields can hold values of different types and sizes. With cell array values, struct expands: struct('v', {1, 2, 3}) is a 1-by-3 struct array. All cell arguments must then have the same size, and non-cell values are copied to every element.
Fields can also be created by assignment, s.age = 30, and removed with rmfield. fieldnames lists them and isfield tests for them.
Input arguments
field- Field name, as text.
value- Any value. If it is a cell array, the result is a struct array with one element per cell; wrap a cell in another cell,
{{...}}, to store it as a single value.
Output arguments
s- Struct or struct array.
Example
Scalar, array and cell value
s = struct('name', 'Ana', 'age', 30)
s.age
t = struct('v', {1, 2, 3});
size(t)
c = struct('c', {{1, 2}});
class(c.c)
Output
s = struct with fields:
name: 'Ana'
age: 30
ans = 30
ans = 1×2
1 3
ans = 'cell'