getfield - Field of structure array - MATLAB (original) (raw)
Syntax
Description
value = getfield([S](#d126e644596),[field](#mw%5F37294266-adb8-4188-9603-13138d790d77))
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 toS(1).field
.
value = getfield([S](#d126e644596),[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)1,...,[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)N)
returns the value stored in a nested structure. For example, if S.a.b.c = 1
, then getfield(S,'a','b','c')
returns1
.
value = getfield([S](#d126e644596),[idx](#mw%5F496b7175-0d50-4e2b-9528-af422948d90b),[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)1,...,[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)N)
specifies the element of the structure array. For example, if S(3,4).a = 1
, then getfield(S,{3,4},'a')
returns1
.
value = getfield([S](#d126e644596),[idx](#mw%5F496b7175-0d50-4e2b-9528-af422948d90b),[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)1,[idx](#mw%5F496b7175-0d50-4e2b-9528-af422948d90b)1,...,[field](#mw%5F37294266-adb8-4188-9603-13138d790d77)N,[idx](#mw%5F496b7175-0d50-4e2b-9528-af422948d90b)N)
specifies elements of fields. For example, if S.a(2) = 1
, thengetfield(S,'a',{2})
returns 1
. Similarly, if S(3,4).a(2).b = 1
, thengetfield(S,{3,4},'a',{2},'b')
returns1
.
Examples
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 = struct with fields: path: 'C:\Temp' m: {'testFunc1.m'} mlapp: {0×1 cell} mlx: {'testFunc2.mlx'} mat: {2×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 = 1×1 cell array {'testFunc2.mlx'}
You also can access a field using dot notation.
value = 1×1 cell array {'testFunc2.mlx'}
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: [1×1 struct] h: 50
While S
is a structure, the fields S.a
, S.a.b
, and S.a.b.e
are also structures.
ans = struct with fields: b: [1×1 struct]
ans = struct with fields: c: 1 d: 'two' e: [1×1 struct]
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')
You also can use dot notation to access the same field.
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 = 5×1 struct array with fields: name folder date bytes isdir datenum
To display information about the 5th file, index into S
.
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')
As an alternative, index into the structure array, and then use dot notation to specify a field.
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=1×2 struct array with fields: a
ans=1×3 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')
You also can use dot notation to access the same field.
Create a structure with a field whose value is an array.
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]})
You also can use dot notation and array indexing to access the same elements.
Input Arguments
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 toS(1,2).a
.
Example: If S.a = [5 10 20]
, thengetfield(S,'a',{[2,3]})
returns [10 20]
.
Extended Capabilities
Usage notes and limitations:
- Field name must be constant.
Version History
Introduced before R2006a