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.

getfield

Field of structure array

Syntax

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

Description

example

value = getfield(S,field) returns the value in the specified field of the structure S. For example, if S.a = 1, then getfield(S,'a') returns 1.

As an alternative to getfield, use dot notation, value = S.field. Dot notation is typically more efficient.

If S is nonscalar, then getfield returns the value in the first element of the array, equivalent to S(1).field.

example

value = getfield(S,field1,...,fieldN) returns the value stored in a nested structure. For example, if S.a.b.c = 1, then getfield(S,'a','b','c') returns 1.

example

value = getfield(S,idx,field1,...,fieldN) specifies the element of the structure array. For example, if S(3,4).a = 1, then getfield(S,{3,4},'a') returns 1.

example

value = getfield(S,idx,field1,idx1,...,fieldN,idxN) specifies elements of fields. For example, if S.a(2) = 1, then getfield(S,'a',{2}) returns 1. Similarly, if S(3,4).a(2).b = 1, then getfield(S,{3,4},'a',{2},'b') returns 1.

Examples

collapse all

Get the value of a field from a structure returned by the what function. what returns a scalar structure with fields containing the path to the specified folder and the names of various kinds of files in the folder.

S = what('C:\Temp')
S = struct with fields:
        path: 'C:\Temp'
           m: {0×1 cell}
       mlapp: {0×1 cell}
         mlx: {0×1 cell}
         mat: {0×1 cell}
         mex: {0×1 cell}
         mdl: {0×1 cell}
         slx: {0×1 cell}
           p: {0×1 cell}
     classes: {0×1 cell}
    packages: {0×1 cell}

Return the names of all Live Scripts listed in the mlx field. When you use the getfield function, you can access a field of the structure returned by a function without using a temporary variable to hold that structure.

value = getfield(what('C:\Temp'),'mlx')
value =

  0×1 empty cell array

You also can access a field using dot notation.

value = S.mlx
value =

  0×1 empty cell array

Access a field of 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.

First, create a nested structure.

S.a.b.c = 1;
S.a.b.d = 'two';
S.a.b.e = struct('f',[3 4],'g','five');
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: 'two'
    e: [1x1 struct]

S.a.b.e
ans = struct with fields:
    f: [3 4]
    g: 'five'

Return the value of S.a.b.d using the getfield function. When you specify a comma-separated list of nested structure names, you must include the structures at every level between the structure at 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'.

value = getfield(S,'a','b','d')
value = 
'two'

You also can use dot notation to access the same field.

value = S.a.b.d
value = 
'two'

Get the value of a field from an element of a structure array returned by the dir function. dir returns a structure array whose elements each contain information about a file in the specified folder.

Return information about files in the folder C:\Temp. There are 5 files in the folder.

S = dir('C:\Temp')
S = 5×1 struct array with fields:
    name
    folder
    date
    bytes
    isdir
    datenum

To display information about the 5th file, index into S.

S(5)
ans = struct with fields:
       name: 'testFunc2.mlx'
     folder: 'C:\Temp'
       date: '19-Jul-2018 09:43:53'
      bytes: 2385
      isdir: 0
    datenum: 7.3726e+05

Return the name of the file described by the 5th element of S using the getfield function. When you use getfield, specify indices in a cell array.

value = getfield(S,{5},'name')
value = 
'testFunc2.mlx'

As an alternative, index into the structure array, and then use dot notation to specify a field.

value = S(5).name
value = 
'testFunc2.mlx'

Access 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 = 1x2 struct array with fields:
    a

S(2).a.b
ans = 1x3 struct array with fields:
    d

Return the value of d from the third element of b using the getfield function. You must specify the indices of both S and b using cell arrays.

value = getfield(S,{2},'a','b',{3},'d')
value = 20

You also can use dot notation to access the same field.

value = S(2).a.b(3).d
value = 20

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]

Return elements of the array using the getfield function. To return a subarray, specify indices after the name of the field. You must specify the indices within a cell array.

value = getfield(S,'a',{[2:4]})
value = 1×3

    10    15    20

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

value = S.a(2:4)
value = 1×3

    10    15    20

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 elements of structure arrays. Indices for field N specify elements of the array in that field, which can be of any type.

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

Example: If S.a = [5 10 20], then getfield(S,'a',{[2,3]}) returns [10 20].

Introduced before R2006a