setfield

Assign value to structure array field

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: [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)'

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: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x)'
    sqrty: [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: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x), with error bar values'
    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 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: [1x100 double]
        y: [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: [1x100 double]
        y: [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.

Introduced before R2006a