structfun — Apply a function to each field of a scalar struct.

structfun(func, S) applies func to each field value of scalar struct S. By default callback results must be scalar-uniform and are packed into a column array in field order.

Syntax

A = structfun(func, S)
A = structfun(func, S, "UniformOutput", tf)
A = structfun(func, S, "ErrorHandler", errfunc)
A = structfun(func, S, "UniformOutput", tf, "ErrorHandler", errfunc)

Inputs

NameTypeRequiredDefaultDescription
funcAnyYesFunction handle or builtin/function name.
SAnyYesScalar struct input.
UniformOutputStringScalarYes"UniformOutput"Name-value key for uniform output mode.
tfLogicalArrayYestrueWhether callback outputs must be scalar-uniform.
ErrorHandlerStringScalarYes"ErrorHandler"Name-value key for callback error handler.
errfuncAnyYesError handler function handle or name.

Returns

NameTypeDescription
AAnyMapped callback outputs.

Returned values from structfun depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:structfun:InvalidInputInput callback, struct argument, or name-value forms are invalid.structfun: invalid input arguments
RunMat:structfun:NotScalarStructSecond argument is not a scalar struct.structfun: second input must be a scalar struct
RunMat:structfun:InternalInternal field vector materialisation fails.structfun: internal error

How structfun works

  • The second input must be a scalar struct. Struct arrays are rejected rather than flattened implicitly.
  • Callback execution uses RunMat's feval path, so function handles, semantic closures, requested output count, UniformOutput, ErrorHandler, and GPU gather behavior follow ordinary callback invocation semantics.
  • With "UniformOutput", true (default), every callback result must be scalar numeric, logical, or complex; results are packed into an N-by-1 array where N is the number of fields.
  • With "UniformOutput", false, callback outputs may have mixed types or sizes and are returned as a scalar struct preserving the input field names.
  • When multiple outputs are requested, each callback must return that many outputs; structfun collects each output independently.
  • "ErrorHandler", handler receives an error struct followed by the field value that triggered the failure.

Does RunMat run structfun on the GPU?

structfun is a host operation. GPU-resident field values are gathered before the callback runs and outputs are host-resident today.

Examples

Compute the length of each struct field

S = struct('a', 1:3, 'b', 10:11);
A = structfun(@length, S)

Expected output:

A =
     3
     2

Collect mixed callback outputs in a struct

S = struct('name', 'runmat', 'flag', true);
C = structfun(@class, S, 'UniformOutput', false)

Expected output:

C =
  struct with fields:
    name: 'char'
    flag: 'logical'

Using structfun with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how structfun changes the result.

Run a small structfun example, explain the result, then change one input and compare the output.

FAQ

Does structfun modify the input struct?

No. It reads the field values, invokes the callback, and returns a new array or cell array.

Can structfun return strings or structs?

Yes, when "UniformOutput" is false. The result is a scalar struct with the same field names as the input. Uniform output mode accepts only scalar numeric, logical, or complex callback results.

Are GPU field values supported?

Yes. GPU-resident field values are gathered before callback invocation; outputs are host-resident today.

Can structfun collect multiple callback outputs?

Yes. When callers request multiple outputs, RunMat requests the same number from every callback invocation and returns one collected output per callback output.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how structfun 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.