getfield — Read a field, nested path, or indexed field value.
getfield provides functional field access for scalar structures, structure arrays, and supported RunMat object-family values.
Syntax
value = getfield(S, field)
value = getfield(S, field1, ..., fieldN)
value = getfield(S, idx, field1, ..., fieldN)
value = getfield(S, idx, field1, idx1, ..., fieldN, idxN)Inputs
S- Type
Any- Required
- Yes
- Description
- Structure or supported RunMat object-family value.
field- Type
PropertyName- Required
- Yes
- Description
- Field name.
path- Type
Any- Required
- Variadic
- Description
- Field names with optional cell-array index selectors.
Returns
value- Type
Any- Description
- Selected field or property value.
Errors
RunMat:getfield:TooManyOutputs- When
- More than one output is requested.
- Message
getfield: too many output arguments
RunMat:getfield:NotEnoughInputs- When
- No field path is supplied.
- Message
getfield: expected at least one field name
RunMat:getfield:FieldExpected- When
- A selector is not followed by a field name.
- Message
getfield: expected field name arguments
Field paths
value = getfield(S,field) reads a named field. Additional names traverse nested structures, so getfield(S,'a','b') is the functional form of S.a.b.
Field names are exact and case-sensitive scalar text. A missing member or a path that reaches a non-structure value produces an error.
Structure arrays and indexed fields
Place an index cell before the first field to select one structure-array element: getfield(S,{row,col},'field'). Place one after a field name to apply ordinary parenthesis indexing to that field's contents. Intermediate selections must identify one value; the final selector may return a subarray. Every index is one-based.
Numeric vectors and logical arrays select subarrays under the indexed value's class and shape rules. Parenthesis indexing of a cell field returns a cell array; it does not extract cell contents. RunMat mode additionally accepts textual positive indices and end inside selector cells.
Objects and resident values
RunMat mode extends getfield to supported object, handle, listener, and exception values. Property access observes declared access rules and invokes an available dependent-property getter.
Reading a field directly preserves the stored value, including an accelerator-resident handle. Indexing a resident field is a separately gated RunMat extension that gathers authoritative storage through its owning provider before host indexing.
Examples
Read a scalar structure field
stats = struct('mean', 42, 'stdev', 3.5);
mu = getfield(stats, 'mean')Expected output:
mu = 42Traverse nested fields
cfg = struct('solver', struct('name', 'cg', 'tolerance', 1e-6));
tol = getfield(cfg, 'solver', 'tolerance')Expected output:
tol = 1.0000e-06Read one structure-array element
records = struct('label', {'first', 'second'}, 'id', {101, 102});
last_id = getfield(records, {2}, 'id')Expected output:
last_id = 102Read selected field elements
s = struct('values', {[10 20 30 40]});
selected = getfield(s, 'values', {[2 4]})Expected output:
selected = [20 40]Read the first structure-array element by default
records = struct('label', {'first', 'second'}, 'id', {101, 102});
first_label = getfield(records, 'label')Expected output:
first_label = 'first'Read a matrix element stored in a field
measurements = struct('values', {[1 2 3; 4 5 6]});
entry = getfield(measurements, 'values', {2, 3})Expected output:
entry = 6Use an integer selector without changing the field class
s = struct('values', {uint16([10 20 30])});
selected = getfield(s, 'values', {uint8([3 1])})Expected output:
selected = uint16([30 10])Using getfield with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how getfield changes the result.
Run a small getfield example, explain the result, then change one input and compare the output.
FAQ
How do I access a nested field?⌄
List every field in order, as in getfield(S,'parent','child').
How do I select a structure-array element?⌄
Put a cell array of one-based indices before the first field name.
Can I index the value stored in a field?⌄
Yes. Put a numeric or logical selector cell immediately after that field name.
Does direct access gather GPU data?⌄
No. The stored resident handle is returned unchanged. Indexing that value requires the RunMat indexed-resident extension and a provider-owned gather.
Do dependent properties run getter methods?⌄
In RunMat mode, supported dependent properties invoke their getter when one is available; object-family access is an explicit extension.
What happens for an invalid handle?⌄
The RunMat object-family extension reports an invalid-handle error before attempting property access.
Related Structs functions
fieldnames · isfield · orderfields · rmfield · setfield · struct · structfun
Source you can read
Every RunMat function's source is public. Read exactly how getfield is executed, line by line, in Rust.
- View the source for getfield in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.