fieldnames — List struct field names, with gated RunMat object introspection.
fieldnames(S) returns a column cell array of character vectors containing the fields stored on a structure. Scalar structures preserve field insertion order. RunMat currently represents structure arrays with cell-backed storage; fieldnames accepts that representation when every element is a scalar structure and returns the sorted union of represented fields. RunMat object-family introspection is available only in RunMat extension mode.
Syntax
names = fieldnames(S)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
S | Any | Yes | — | Struct, cell-backed struct array, or supported RunMat object input. |
Returns
| Name | Type | Description |
|---|---|---|
names | Any | Cell array of field names. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fieldnames:InvalidTarget | Input is not a struct, cell-backed struct array, or supported RunMat object value. | fieldnames: expected struct, struct array, or supported object |
RunMat:fieldnames:StructArrayContents | Cell-backed struct-array input contains a non-struct element. | fieldnames: expected struct array contents to be structs |
RunMat:fieldnames:InternalError | Building the output cell array or internal metadata processing fails. | fieldnames: internal error |
How fieldnames works
- Works with scalar structs and RunMat's current cell-backed struct-array representation. A nonempty cell-backed input rejects if any element is not a scalar struct.
- Returns a column cell array (
n × 1) whose elements are character vectors (1 × N char arrays). - Scalar-struct field names preserve the structure's insertion order and remain case-sensitive (
nameandNameare distinct). - Cell-backed struct arrays return a deterministic, alphabetically sorted union of their represented field names.
- In RunMat extension mode, value objects, handle objects, and listeners expose RunMat's currently available property metadata. This surface is not claimed as complete MATLAB object introspection.
- Empty scalar structs and empty cell-backed struct arrays yield an empty
0 × 1cell array. GPU-resident field values nested inside a struct stay on the device becausefieldnamesonly inspects host metadata. - Integer, logical, floating-point, and resident numeric arrays are not applicable inputs and reject without numeric or text coercion.
Does RunMat run fieldnames on the GPU?
fieldnames is metadata-only. It does not launch kernels or gather data. GPU tensors or handles that live inside structs or objects remain device-resident, and the builtin simply walks the host-side descriptors that remember their names.
GPU memory and residency
You do not need to call gpuArray when using fieldnames. RunMat leaves each value—CPU or GPU—where it already resides. When structs or objects contain GPU handles, those handles remain valid and stay resident on the device throughout the introspection call.
Examples
Listing the fields of a scalar structure
s = struct("name", "Ada", "score", 42);
fields = fieldnames(s)Expected output:
fields =
{'name'}
{'score'}Inspecting field names before accessing values
stats = struct("min", 1.2, "max", 9.8);
if any(strcmp(fieldnames(stats), "median"))
disp(stats.median);
else
disp("median not available");
endExpected output:
median not availableGathering represented field names from a struct array
people = struct("name", {"Ada", "Grace"}, "id", {101, 102});
fields = fieldnames(people)Expected output:
fields =
{'id'}
{'name'}Listing represented object properties
% Save this class in Counter.m before running the example:
% classdef Counter
% properties
% Value = 0
% Step = 1
% end
% end
c = Counter;
props = fieldnames(c)Expected output:
props =
{'Step'}
{'Value'}Discovering fields inside nested structs
config.database = struct("host", "db.local", "port", 5432);
names = fieldnames(config.database)Expected output:
names =
{'host'}
{'port'}Handling empty structs safely
emptyScalar = struct();
emptyArray = struct("id", {});
fs = fieldnames(emptyScalar);
fa = fieldnames(emptyArray)Expected output:
fs =
0x1 empty cell array
fa =
0x1 empty cell arrayUsing fieldnames with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fieldnames changes the result.
Run a small fieldnames example, explain the result, then change one input and compare the output.
FAQ
What does fieldnames return?⌄
For a scalar struct, it returns a column cell array of character vectors in stored field order. For RunMat's cell-backed struct arrays, it returns the sorted union of represented fields. RunMat extension mode also exposes the property metadata currently represented for supported object families.
Are the field names sorted?⌄
Scalar-struct names preserve the order in which fields were inserted. Cell-backed struct-array names are sorted after their union is collected. RunMat object-family extensions currently use their own deterministic property ordering.
Does fieldnames gather GPU data back to the CPU?⌄
No. The builtin only inspects host-side metadata; GPU-resident tensors or handles remain untouched.
Can fieldnames inspect struct arrays?⌄
Yes, for RunMat's current cell-backed struct-array representation. Every nonempty element must be a scalar struct. Because this backing is shared with ordinary cell arrays, a cell containing only structs is currently interpreted as a represented struct array by fieldnames; a future discriminated struct-array value can remove that representation ambiguity.
Does fieldnames work with objects and handle objects?⌄
Only in RunMat extension mode. The builtin merges the object property metadata currently represented by RunMat, but this is an explicit extension and not a claim of complete MATLAB object-property policy.
What happens with empty structs or empty struct arrays?⌄
An empty scalar struct or empty cell-backed struct array returns an empty column cell array (0 × 1).
Can I use fieldnames on unsupported inputs?⌄
No. Compatibility mode accepts scalar structures and cell-backed arrays whose elements are all scalar structures. Numeric arrays and cells containing non-struct elements reject, and supported object families require RunMat extension mode.
Related Structs functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fieldnames is executed, line by line, in Rust.
- View the source for fieldnames in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.