Accelerating the pace of engineering and science

Documentation Center

• Trial Software

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.

• 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 all are scalar cell arrays, then s is a scalar structure.

• If any of the value inputs are nonscalar cell arrays, then s has the same dimensions as the nonscalar cell arrays. 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.

s = struct([]) creates an empty (0-by-0) structure with no fields.

s = struct(obj) creates a structure with field names and values that correspond to properties of obj. MATLAB® 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

expand all

Structure with One Field

Create a nonscalar structure with one field, f.

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

View the contents 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'```

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 Array

Create an empty structure with several fields.

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

Nested Structure Array

Create a nested structure: a contains field b, which in turn contains fields c and d.

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

View the names of the fields of nested structure a.b.

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

Input Arguments

expand 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 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 all of the value inputs are scalar cell arrays, 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.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | struct | cell | function_handle
Complex Number Support: Yes

obj — Objectany nonfundamental class

Object of any class, except the fundamental data types such as double or char.