# setfield

Assign value to structure array field

## Syntax

``S = setfield(S,field,value)``
``S = setfield(S,field1,...,fieldN,value)``
``S = setfield(S,idx,field1,...,fieldN,value)``
``S = setfield(S,idx,field1,idx1,...,fieldN,idxN,value)``

## Description

example

````S = setfield(S,field,value)` assigns a value to the specified field of the structure `S`. For example, `S = setfield(S,'a',1)` makes the assignment ```S.a = 1```.As an alternative to `setfield`, use dot notation: ```S.field = value```. Dot notation is typically more efficient.If `S` does not have the specified field, then `setfield` creates it and assigns `value` to it.```

example

````S = setfield(S,field1,...,fieldN,value)` assigns a value to the specified field of a nested structure. For example, ```S = setfield(S,'a','b','c',1)``` makes the assignment `S.a.b.c = 1`, where the fields `S.a` and `S.a.b` are also structures.```

example

````S = setfield(S,idx,field1,...,fieldN,value)` specifies an element of `S` and assigns a value to one of its fields. For example, `S = setfield(S,{3,4},'a',1)` makes the assignment `S(3,4).a = 1`.```

example

````S = setfield(S,idx,field1,idx1,...,fieldN,idxN,value)` specifies elements of fields. For example, `S = setfield(S,'a',{2},1)` makes the assignment `S.a(2) = 1`. Similarly, ```S = setfield(S,{3,4},'a',{2},'b',1)``` makes the assignment ```S(3,4).a(2).b = 1```.```

## Examples

collapse all

Create a scalar structure.

```S.x = linspace(0,2*pi); S.y = sin(S.x); S.title = ''```
```S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: '' ```

Assign a value to a field using the `setfield` function.

`S = setfield(S,'title','y = sin(x)')`
```S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)' ```

Assign a value to another field. If you specify a field that does not exist, then `setfield` creates it.

```e = sqrt(abs(S.y)); S = setfield(S,'sqrty',e)```
```S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)' sqrty: [0 0.2518 0.3558 0.4350 0.5011 0.5586 0.6096 0.6556 0.6973 0.7353 0.7700 0.8017 0.8307 0.8571 0.8810 0.9025 0.9218 0.9389 0.9537 0.9665 0.9772 0.9858 0.9924 0.9969 0.9994 0.9999 0.9984 0.9949 0.9893 0.9818 0.9721 0.9604 ... ] (1x100 double) ```

You also can assign a value to a field using dot notation.

`S.title = 'y = sin(x), with error bar values'`
```S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x), with error bar values' sqrty: [0 0.2518 0.3558 0.4350 0.5011 0.5586 0.6096 0.6556 0.6973 0.7353 0.7700 0.8017 0.8307 0.8571 0.8810 0.9025 0.9218 0.9389 0.9537 0.9665 0.9772 0.9858 0.9924 0.9969 0.9994 0.9999 0.9984 0.9949 0.9893 0.9818 0.9721 0.9604 ... ] (1x100 double) ```

Create a nested structure. In a nested structure, a structure at any level can have fields that are structures, and other fields that are not structures.

```S.a.b.c = 1; S.a.b.d = 2; S.a.b.e = struct('f',[3 4],'g',5); S.h = 50```
```S = struct with fields: a: [1x1 struct] h: 50 ```

While `S` is a structure, the fields `S.a`, `S.a.b`, and `S.a.b.e` are also structures.

`S.a`
```ans = struct with fields: b: [1x1 struct] ```
`S.a.b`
```ans = struct with fields: c: 1 d: 2 e: [1x1 struct] ```
`S.a.b.e`
```ans = struct with fields: f: [3 4] g: 5 ```

Assign a value to `S.a.b.d` using the `setfield` function. When you specify a comma-separated list of nested structure names, include the structure names at every level between the top and the field name you specify. In this case, the comma-separated list of structure names is `'a','b'` and the field name is `'d'`.

```S = setfield(S,'a','b','d',1024); S.a.b```
```ans = struct with fields: c: 1 d: 1024 e: [1x1 struct] ```

You also can use dot notation to assign a value.

```S.a.b.d = 2048; S.a.b```
```ans = struct with fields: c: 1 d: 2048 e: [1x1 struct] ```

Assign values to fields of elements of a structure array.

First, create a structure array. As in all structure arrays, each element is a structure with the same fields.

```S.x = linspace(0,2*pi); S.y = sin(S.x); S(2).x = S.x; S(2).y = cos(S(2).x)```
```S=1×2 struct array with fields: x y ```

You also can assign values using `setfield`. If a field does not exist, `setfield` creates it. Create a field named `title`.

`S = setfield(S,{1},'title','y = sin(x)')`
```S=1×2 struct array with fields: x y title ```

The `setfield` function assigns a value to a field of an individual element, but the output argument is the entire structure array.

Display the first element of `S`.

`S(1)`
```ans = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)' ```

As an alternative, index into the structure array, and then use dot notation to assign a value to a field of an element.

```S(2).title = 'y = cos(x)'; S(2)```
```ans = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [1 0.9980 0.9920 0.9819 0.9679 0.9501 0.9284 0.9029 0.8738 0.8413 0.8053 0.7660 0.7237 0.6785 0.6306 0.5801 0.5272 0.4723 0.4154 0.3569 0.2969 0.2358 0.1736 0.1108 0.0476 -0.0159 -0.0792 -0.1423 -0.2048 -0.2665 -0.3271 ... ] (1x100 double) title: 'y = cos(x)' ```

Assign a value to a field of a nested structure, in which the structures at some levels are structure arrays. In this example, `S` is a 1-by-2 structure array. The second element, `S(2)`, has a nested structure `a.b`, where `b` is a 1-by-3 structure array.

First, create a nested structure. After creating the structure using dot notation, create another nonscalar structure array using the `struct` function and add it as a field.

```S.a = 1; S(2).a.b = struct('d',{5,10,20}); S```
```S=1×2 struct array with fields: a ```
`S(2).a.b`
```ans=1×3 struct array with fields: d ```

Display the third element of `S(2).a.b`.

`S(2).a.b(3)`
```ans = struct with fields: d: 20 ```

Assign a new value to the field `d` of `S(2).a.b(3)` using the `setfield` function. Display the structure with the updated field.

```S = setfield(S,{2},'a','b',{3},'d',3.1416); S(2).a.b(3)```
```ans = struct with fields: d: 3.1416 ```

Create a structure with a field whose value is an array.

`S.a = [5 10 15 20 25]`
```S = struct with fields: a: [5 10 15 20 25] ```

Assign values to elements of `S.a` using the `setfield` function. To assign values to particular elements, specify indices after the name of the field. You must specify the indices within a cell array. However, specify the new values in an array whose data type matches the data type of the field.

`S = setfield(S,'a',{3:5},[0 -50 -100])`
```S = struct with fields: a: [5 10 0 -50 -100] ```

You also can use dot notation and array indexing to assign values to the same elements.

`S.a(3:5) = [20 40 80]`
```S = struct with fields: a: [5 10 20 40 80] ```

## Input Arguments

collapse all

Structure array. If `S` is nonscalar, then each element of `S` is a structure, and all elements have the same fields with the same names.

Field name, specified as a character vector or string scalar.

Indices, specified as a cell array of numeric or logical values. Indices for `S` and fields `1` through `N-1` specify individual elements of structure arrays. Indices for field `N` specify one or more elements of the array in that field, which can be of any type.

Example: `S = setfield(S,{1,2},'a',1)` is equivalent to `S(1,2).a = 1`.

Example: If `S.a = [5 10 20]`, then ```S = setfield(S,'a',{[2,3]},[50 100])``` is equivalent to ```S.a(2:3) = [50 100]```.

Values, specified as any type of array having any size.

## Version History

Introduced before R2006a