MATLAB Function Reference
struct

Create structure array

Syntax

• ```s = struct('field1',{},'field2',{},...)
s = struct('field1',values1,'field2',values2,...)
```

Description

```s = struct('field1',{},'field2',{},...) ``` creates an empty structure with fields `field1`, `field2`, `...`

```s = struct('field1',values1,'field2',values2,...) ``` creates a structure array with the specified fields and values. The value arrays `values1`, `values2`, etc. must be cell arrays of the same size or scalar cells. Corresponding elements of the value arrays are placed into corresponding structure array elements. The size of the resulting structure is the same size as the value cell arrays or 1-by-1 if none of the values is a cell.

Examples

The command

• ```s = struct('type',{'big','little'},'color',{'red'},'x',{3 4})
```

produces a structure array `s`:

• ```s =
1x2 struct array with fields:
type
color
x
```

The value arrays have been distributed among the fields of `s`:

• ```s(1)
ans =
type: 'big'
color: 'red'
x: 3
s(2)
ans =
type: 'little'
color: 'red'
x: 4
```

Similarly, the command

• ```a.b = struct('z',{});
```

produces an empty structure `a.b` with field `z`.

• ```a.b
ans =
0x0 struct array with fields:
z
```

`isstruct`, `fieldnames`, `isfield`, `orderfields`, `rmfield`, `deal`, `cell2struct`, `struct2cell`