isfield
Determine whether input is structure array field
Syntax
tf = isfield(S, 'fieldname')
tf = isfield(S, C)
Description
tf = isfield(S, 'fieldname')
examines structureS
to see if it includes the field specified by the quoted character vector'fieldname'
. Outputtf
is set to logical1
(true
) ifS
contains the field, or logical0
(false
) if not. IfS
is not a structure array,isfield
returnsfalse
.
tf = isfield(S, C)
examines structureS
for multiple fieldnames as specified in cell array of character vectorsC
, and returns an array of logical values to indicate which of these fields are part of the structure. Elements of output arraytf
are set to a logical 1 (true
) if the corresponding element ofC
holds a fieldname that belongs to structureS
. Otherwise, logical 0 (false
) is returned in that element. In other words, if structureS
contains the field specified inC{m,n}
,isfield
returns a logical 1 (true
) intf(m,n)
.
Note
isfield
returnsfalse
if thefield
orfieldnames
input is empty.
Examples
Example 1 — Single Fieldname Syntax
Given the following MATLAB®structure,
patient.name = 'John Doe'; patient.billing = 127.00; patient.test = [79 75 73; 180 178 177.5; 220 210 205];
isfield
identifiesbilling
as a field of that structure.
isfield(patient,'billing') ans = 1
Example 2 — Multiple Fieldname Syntax
Check structureS
for any of four possible fieldnames. Only the first is found, so the first element of the return value is set totrue
:
S = struct('one', 1, 'two', 2); fields = isfield(S, {'two', 'pi', 'One', 3.14}) fields = 1 0 0 0