isfield
Tests whether a struct has the given field names.
Category: Language Fundamentals > Data Types
Syntax
tf = isfield(S, name)
Returns true if S is a struct with a field called name.
tf = isfield(S, names)
Checks several names at once and returns one logical per name.
Description
isfield compares names exactly, including case. It never errors on a non-struct first argument, it just answers false, which makes it safe for checking optional fields of unknown input.
A name that is not text, inside the cell array, also yields false for that position instead of an error.
Input arguments
S- Value to test. If it is not a struct the result is
false. name, names- Character vector, string scalar, or cell array of names.
Output arguments
tf- Logical scalar for one name, or a logical array the same size as the cell array
names.
Example
Checking one and several fields
s.a = 1;
s.b = 'x';
isfield(s, 'a')
isfield(s, {'a', 'c', 'b'})
isfield(5, 'a')
Output
ans = logical
1
ans = 1×3 logical array
1 0 1
ans = logical
0