This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

setfield

Assign value to structure array field

Syntax

S = setfield(S,field,value)
S = setfield(S,level1,...,levelN,field,value)
S = setfield(S,structIndices,field,value)
S = setfield(S,structIndices,level1,...,levelN,field,value)
S = setfield(S,structIndices,level1,level1Indices,...,levelN,levelNIndices,field,value)
S = setfield(___,field,fieldIndices,value)

Description

example

S = setfield(S,field,value) assigns a value to the specified field of the 1-by-1 structure S. For example, S = setfield(S,'a',1) makes the assignment S.a = 1.

If S does not have the specified field, then setfield adds it and assigns value to it.

example

S = setfield(S,level1,...,levelN,field,value) assigns a value to the specified field of a nested structure. In a nested structure, the value of a field can be another 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 at the first and second levels of nesting.

example

S = setfield(S,structIndices,field,value) specifies an element of S using the cell array structIndices when S is a structure array. For example, S = setfield(S,{3},'a',1) makes the assignment S(3).a = 1, where S(3) is the third element of S.

S = setfield(S,structIndices,level1,...,levelN,field,value) specifies a field of a nested structure when S is a structure array. For example, S = setfield(S,{3,4},'a','b','c',1) makes the assignment S(3,4).a.b.c = 1.

example

S = setfield(S,structIndices,level1,level1Indices,...,levelN,levelNIndices,field,value) specifies elements of S, levelN, and structure arrays nested at any level in between, when any or all of the structures can be nonscalar.

example

S = setfield(___,field,fieldIndices,value) assigns the elements of value to elements of the array stored in field and specified by fieldIndices. You can use this syntax with the nested levels and structure array indices specified in any of the previous syntaxes.

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: [1x100 double]
        y: [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: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x)'

Add another value to S. If you specify a field that does not exist, then setfield adds it.

e = sqrt(abs(S.y));
S = setfield(S,'sqrty',e)
S = struct with fields:
        x: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x)'
    sqrty: [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 syntax 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 = 1x2 struct array with fields:
    x
    y

You also can assign values using setfield. If a field does not exist, setfield adds it. Add a field named title to S.

S = setfield(S,{1},'title','y = sin(x)');
S = setfield(S,{2},'title','y = cos(x)')
S = 1x2 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: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(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 indexing, 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 = 1x2 struct array with fields:
    a

S(2).a.b
ans = 1x3 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 syntax 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

Input 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. 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.

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

Indices into the structure array, specified as a cell array containing integers or logical values.

Example: If S(2,3).a = 2, then specify structIndices as {2,3} to access the structure at S(2,3).

Names of the structures in a nested structure, specified as a comma-separated list of character vectors or string scalars.

The structure level1 is a field of S, and also is a structure at the first level of nesting. The structure levelN is a structure at the Nth level of nesting. You must specify the names of the structures at all levels between S and levelN. The input argument field is a field of levelN.

Example: If S.a.b.c = 1, then the list 'a','b' specifies two structures nested under S, with c being a field of the structure b.

Indices specifying an element of a nested structure array at the Nth level, specified as a cell array containing integers or logical values.

In a nested structure, a structure at any level can be a structure array. If the structure specified by levelN is a structure array, then levelNIndices specify an element of levelN.

Example: If S.a(2).b(3).c = 2, then specify level1Indices as {2} and level2Indices as {3} to access S.a(2).b(3).

Indices into a field, specified as a cell array containing integers, ranges, or logical values.

To index into all elements of the array stored in the field, specify fieldIndices as {':'}.

Example: If S.a = [5 10 20 40], then specify fieldIndices as {2:4} to access the second, third, and fourth elements of the array.

Tips

  • Usually, it is simpler to assign values to fields using dot syntax rather than the setfield function. Use setfield to:

    • Avoid creating unnecessary temporary variables.

    • Assign values to fields of nested structures.

Introduced before R2006a