What does the isfield function do in MATLAB / RunMat?
tf = isfield(S, name) returns a logical value that indicates whether the struct S
defines the field name. When name is a string array or a cell array of names,
isfield returns a logical array with the same size, reporting the result for each
requested field.
How does the isfield function behave in MATLAB / RunMat?
- Works with scalar structs and struct arrays created with
struct,load, or similar builtins. - Accepts character vectors, string scalars, string arrays, or cell arrays containing character vectors and/or string scalars for the
nameargument. - Returns a scalar logical (
true/false) whennameis a single string or char vector. - Returns a logical array that matches the size of
namewhen it is a string array or cell array. - If the first argument is not a struct or struct array, the result is
false(or a logical array offalsevalues) without raising an error. - Empty struct arrays yield
falsefor every queried name because they do not carry field metadata once the elements have been removed.
isfield Function GPU Execution Behaviour
isfield performs metadata checks entirely on the host. It never gathers or copies GPU
tensors that may be stored inside the struct; it only inspects the host-side descriptors
that record field names. No acceleration provider hooks are required.
Examples of using the isfield function in MATLAB / RunMat
Checking whether a struct defines a single field
s = struct("name", "Ada", "score", 42);
hasScore = isfield(s, "score");
Expected output:
hasScore =
logical
1
Testing multiple field names at once
s = struct("name", "Ada", "score", 42);
names = {"name", "department"; "score", "email"};
mask = isfield(s, names);
Expected output:
mask =
2×2 logical array
1 0
1 0
Inspecting a struct array that shares a common schema
people = struct("name", {"Ada", "Grace"}, "id", {101, 102});
idxMask = isfield(people, ["id", "department"]);
Expected output:
idxMask =
1×2 logical array
1 0
Using isfield before accessing optional configuration fields
cfg = struct("mode", "fast");
if ~isfield(cfg, "rate")
cfg.rate = 60;
end
Mixing string scalars and character vectors in a cell array
opts = struct("Solver", "cg", "MaxIter", 200);
queries = {"Solver", "Tolerances"; "MaxIter", "History"};
present = isfield(opts, queries);
Behaviour when the input is not a struct
value = 42;
tf = isfield(value, "anything");
Expected output:
tf =
logical
0
GPU residency in RunMat (Do I need gpuArray?)
isfield is metadata-only. It neither moves nor inspects the contents of GPU tensors that
might live inside the struct. You do not need to call gpuArray or gather to use the
builtin; residency is preserved automatically.
FAQ
What argument types does isfield accept for field names?
Character vectors, string scalars, string arrays, and cell arrays that contain character vectors or string scalars. Passing other types raises an error.
Does isfield error when the first input is not a struct?
No. It returns false (or a logical array of false) to mirror MATLAB's behaviour. Use
isstruct if you need to guard against non-struct inputs before calling isfield.
How do struct arrays affect the result?
Every element of the struct array must contain the queried field. If any element is
missing the field, the result is false for that name.
What happens with empty struct arrays?
Empty struct arrays do not retain field metadata in RunMat yet, so isfield returns
false for all queries. This matches MATLAB when the struct has no defined fields.
Are field name comparisons case-sensitive?
Yes. Field names are compared using exact, case-sensitive matches, just like MATLAB.
Does isfield gather GPU data?
No. The builtin only inspects field metadata and leaves GPU-resident tensors untouched.