struct

Create structure array

Syntax

• ``s = struct``
• ``s = struct(field,value)``
example
• ``s = struct(field1,value1,...,fieldN,valueN)``
example
• ``s = struct([])``
• ``s = struct(obj)``

Description

````s = struct` creates a scalar (1-by-1) structure with no fields.```

example

````s = struct(field,value)` creates a structure array with the specified field and values. The `value` input argument can be any data type, such as a numeric, logical, character, or cell array.If `value` is not a cell array, then `s` is a scalar structure, where ```s.(field) = value```.If `value` is a cell array, then `s` is a structure array with the same dimensions as `value`. Each element of `s` contains the corresponding element of `value`. For example, `s = struct('f',{'a','b'})` returns ```s(1).f = 'a'``` and `s(2).f = 'b'`.If `value` is an empty cell array `{}`, then `s` is an empty (0-by-0) structure.```

example

````s = struct(field1,value1,...,fieldN,valueN)` creates a structure array with multiple fields. Any nonscalar cell arrays in the set `value1,...,valueN` must have the same dimensions.If none of the `value` inputs is a cell array, or if all `value` inputs that are cell arrays are scalars, then `s` is a scalar structure.If any of the `value` inputs is a nonscalar cell array, then `s` has the same dimensions as the nonscalar cell array. For any `value` that is a scalar cell array or an array of any other data type, `struct` inserts the contents of `value` in the relevant field for all elements of `s`.If any `value` input is an empty cell array, `{}`, then output `s` is an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use `[]` as a `value` input instead.```
````s = struct([])` creates an empty (0-by-0) structure with no fields.```
````s = struct(obj)` creates a scalar structure with field names and values that correspond to properties of `obj`. The `struct` function does not convert `obj`, but rather creates `s` as a new structure. This structure does not retain the class information, so private, protected, and hidden properties become public fields in `s`. The `struct` function issues a warning when you use this syntax.```

Examples

collapse all

Structure with One Field

Create a nonscalar structure with one field.

```field = 'f'; value = {'some text'; [10, 20, 30]; magic(5)}; s = struct(field,value)```
```s = 3x1 struct array with fields: f```

View the contents of each element.

`s.f`
```ans = some text ans = 10 20 30 ans = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9```

When you access a field of a nonscalar structure, such as `s.f`, MATLAB® returns a comma-separated list. In this case, `s.f` is equivalent to `s(1).f, s(2).f, s(3).f`.

Structure with Multiple Fields

Create a nonscalar structure with several fields.

```field1 = 'f1'; value1 = zeros(1,10); field2 = 'f2'; value2 = {'a', 'b'}; field3 = 'f3'; value3 = {pi, pi.^2}; field4 = 'f4'; value4 = {'fourth'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4)```
```s = 1x2 struct array with fields: f1 f2 f3 f4```

The cell arrays for `value2` and `value3` are 1-by-2, so `s` is also 1-by-2. Because `value1` is a numeric array and not a cell array, both `s(1).f1` and `s(2).f1` have the same contents. Similarly, because the cell array for `value4` has a single element, `s(1).f4` and `s(2).f4` have the same contents.

`s(1)`
```ans = f1: [0 0 0 0 0 0 0 0 0 0] f2: 'a' f3: 3.1416 f4: 'fourth'```
`s(2)`
```ans = f1: [0 0 0 0 0 0 0 0 0 0] f2: 'b' f3: 9.8696 f4: 'fourth'```

Structure with Empty Field

Create a structure with an empty field. Use `[]` to specify the value of the empty field.

```s = struct('f1','a','f2',[]) ```
```s = f1: 'a' f2: [] ```

Fields That Contain Cell Arrays

Create a structure with a field that contains a cell array.

```field = 'mycell'; value = {{'a','b','c'}}; s = struct(field,value)```
```s = mycell: {'a' 'b' 'c'}```

Empty Structure

Create an empty structure with several fields.

`s = struct('a',{},'b',{},'c',{})`
```s = 0x0 struct array with fields: a b c```

Assign a value to a field in an empty structure.

`s(1).a = 'a'`
```s = a: 'a' b: [] c: []```

Nested Structure

Create a nested structure. `a` is a structure with a field which contains another structure.

`a.b = struct('c',{},'d',{})`
```a = b: [0x0 struct]```

View the names of the fields of `a.b`.

`fieldnames(a.b)`
```ans = 'c' 'd'```

Input Arguments

collapse all

`field` — Field namestring

Field name, specified as a string. Valid field names begin with a letter, and can contain letters, digits, and underscores. The maximum length of a field name is the value that the `namelengthmax` function returns.

`value` — Values within structure fieldcell array | scalar | vector | multidimensional array

Values within a structure field, specified as a cell array or as a scalar, vector, or multidimensional array of any other data type.

If none of the `value` inputs is a cell array, or if all `value` inputs that are cell arrays are scalars, then output `s` is a scalar structure. Otherwise, `value` inputs that are nonscalar cell arrays must have the same dimensions, and output `s` also has those dimensions. For any `value` that is a scalar cell array or an array of any other data type, `struct` inserts the contents of `value` in the relevant field for all elements of `s`.

If any `value` input is an empty cell array, `{}`, then output `s` is an empty structure array. To specify an empty field and keep the values of the other fields, use `[]` as a `value` input instead.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical` | `char` | `struct` | `table` | `cell` | `function_handle` | `categorical` | `datetime` | `duration` | `calendarDuration`
Complex Number Support: Yes

`obj` — Objectobject of any nonfundamental class

Object. `struct` copies the properties of `obj` to the fields of a new scalar structure. `obj` cannot be an object of a fundamental data type, such as `double` or `char`. See Fundamental MATLAB Classes for the list of fundamental data types.