RunMat
GitHub

fieldnames — List struct or object field names in MATLAB and RunMat.

fieldnames(S) returns a column cell array of character vectors containing field names defined on S. Struct arrays return the union of field names across elements, and object/property handling follows MATLAB semantics with deterministic, case-sensitive output.

Syntax

names = fieldnames(S)

Inputs

NameTypeRequiredDefaultDescription
SAnyYesStruct, struct array, or object input.

Returns

NameTypeDescription
namesAnyCell array of field names.

Errors

IdentifierWhenMessage
RunMat:fieldnames:InvalidTargetInput is not a struct, struct array, or object value.fieldnames: expected struct, struct array, or object
RunMat:fieldnames:StructArrayContentsStruct-array backing cell contains non-struct elements.fieldnames: expected struct array contents to be structs
RunMat:fieldnames:InternalErrorBuilding the output cell array or internal metadata processing fails.fieldnames: internal error

How fieldnames works

  • Works with scalar structs as well as struct arrays created by struct, load, or other builtins.
  • Returns a column cell array (n × 1) whose elements are character vectors (1 × N char arrays).
  • Objects (value or handle) report the union of their public, non-static class properties together with any dynamic properties stored on the instance.
  • For struct arrays, the union of field names across all elements is reported.
  • Field names are sorted alphabetically and remain case-sensitive (name and Name are distinct).
  • Empty structs yield an empty 0 × 1 cell array. GPU-resident field values stay on the device— fieldnames only inspects metadata.

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");
end

Expected output:

median not available

Gathering field names from a struct array

people = struct("name", {"Ada", "Grace"}, "id", {101, 102});
fields = fieldnames(people)

Expected output:

fields =
    {'id'}
    {'name'}

Listing the properties of an object

% 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 array

Using 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?

It returns a column cell array of character vectors for every field or property defined on the input struct, struct array, or object instance.

Are the field names sorted?

Yes. RunMat stores fields and properties in hash maps, so fieldnames sorts them alphabetically to produce a stable, deterministic result.

Does fieldnames gather GPU data back to the CPU?

No. The builtin only inspects host-side metadata; GPU-resident tensors or handles remain untouched.

How do struct arrays affect the result?

All struct elements are examined and the union of their field names is returned. Duplicate names are collapsed automatically.

Does fieldnames work with objects and handle objects?

Yes. The builtin merges public, non-static class properties with any dynamic properties stored on the instance. Handle objects reuse the same logic even when their payloads are shared across references.

What happens with empty structs or empty struct arrays?

The result is an empty column cell array (0 × 1). This matches MATLAB behaviour for empty scalars and for struct arrays that contain no elements.

Can I use fieldnames on unsupported inputs?

No. Passing anything other than a struct, struct array, or object raises fieldnames: expected struct, struct array, or object.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fieldnames is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.