RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • fieldnames
    • getfield
    • isfield
    • orderfields
    • rmfield
    • setfield
    • struct
    • structfun

struct — Create scalar structs or struct arrays from field/value inputs with MATLAB-compatible construction rules.

S = struct(...) creates scalar structs or struct arrays by pairing field names with values. It supports MATLAB-compatible name/value, struct-copy, and cell-expansion construction forms.

Syntax

S = struct()
S = struct(template)
S = struct(field, value, ...)

Inputs

NameTypeRequiredDefaultDescription
templateAnyYes—Existing struct/array template or empty array.
fieldPropertyNameYes—Field name.
valueAnyYes—Field value or cell array.
name_value_pairsAnyVariadic—Additional field/value pairs.

Returns

NameTypeDescription
SAnyScalar struct or struct array.

Errors

IdentifierWhenMessage
RunMat:struct:InvalidSingleInputSingle input is not a valid struct template.struct: expected name/value pairs, an existing struct or struct array, or [] to create an empty struct array
RunMat:struct:NameValuePairsArguments are not complete pairs.struct: expected name/value pairs
RunMat:struct:CellSizeMismatchCell values have mismatched shapes.struct: cell inputs must have matching sizes
RunMat:struct:SizeOverflowRequested size exceeds limits.struct: struct array size exceeds platform limits
RunMat:struct:AssembleFailedInternal assembly failed.struct: failed to assemble struct array
RunMat:struct:EmptyArrayFailedEmpty array creation failed.struct: failed to create empty struct array
RunMat:struct:StructArrayContentsCell contains non-struct values.struct: single argument cell input must contain structs
RunMat:struct:StructArrayCopyFailedStruct array copy failed.struct: failed to copy struct array
RunMat:struct:FieldNameTypeField name has invalid type.struct: field names must be strings or character vectors
RunMat:struct:FieldNameScalarField name is not scalar.struct: field names must be scalar string arrays or character vectors
RunMat:struct:FieldNameCharVectorCharacter field name is not a row vector.struct: field names must be 1-by-N character vectors
RunMat:struct:FieldNameEmptyField name is empty.struct: field names must be nonempty
RunMat:struct:FieldNameStartCharField name is not a valid identifier.struct: field names must be valid MATLAB identifiers

How struct works

  • Field names must satisfy the MATLAB isvarname rules: they start with a letter, contain only letters, digits, or underscores, stay within namelengthmax, and are not MATLAB keywords.
  • The last occurrence of a repeated field name wins and overwrites earlier values.
  • String scalars, character vectors, and single-element string arrays are accepted as field names.
  • struct() returns a scalar struct with no fields, while struct([]) yields a 0×0 struct array.
  • When any value input is a cell array, every cell array input must share the same size. Non-cell inputs are replicated across every element of the resulting struct array.
  • Numeric field values retain their exact class, shape, and storage. GPU-resident field values remain resident handles rather than being gathered during construction.
  • Passing an existing struct or struct array (struct(S)) creates a deep copy; the original data is untouched.

Does RunMat run struct on the GPU?

struct performs all bookkeeping on the host. GPU-resident values—such as tensors created with gpuArray—are stored as-is inside the resulting struct or struct array. No kernels are launched and no data is implicitly gathered back to the CPU.

GPU memory and residency

Usually not. RunMat's planner keeps GPU values resident as long as downstream operations can profit from them. You can still seed GPU residency explicitly with gpuArray for MATLAB compatibility; the handles remain untouched inside the struct until another builtin decides to gather or operate on them.

Examples

Creating a simple structure for named fields

s = struct("name", "Ada", "score", 42);
disp(s.name);
disp(s.score)

Expected output:

Ada
42

Building a struct array from paired cell inputs

names = {"Ada", "Grace"};
ages = {36, 45};
people = struct("name", names, "age", ages);
{people.name}

Expected output:

{'Ada'}    {'Grace'}

Broadcasting scalars across a struct array

ids = struct("id", {101, 102, 103}, "department", "Research");
{ids.department}

Expected output:

{'Research'}    {'Research'}    {'Research'}

Copying an existing structure

a = struct("id", 7, "label", "demo");
b = struct(a);
b.id = 8;
disp([a.id b.id])

Expected output:

7     8

Building an empty struct array

s = struct([]);
disp(size(s))

Expected output:

0     0

Using struct with coding agents

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

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

FAQ

Do field names have to be valid identifiers?⌄

Yes. RunMat mirrors MATLAB and requires names to satisfy isvarname. Names must begin with a letter, may contain letters, digits, and underscores, must stay within namelengthmax, and cannot be MATLAB keywords.

How do I create a struct array?⌄

Provide one or more value arguments as cell arrays with identical sizes. Each cell contributes the value for the corresponding struct element. Non-cell values are replicated across all elements.

What happens when the same field name appears more than once?⌄

The last value wins; earlier values for the same field are overwritten.

Does struct gather GPU data back to the CPU?⌄

No. GPU tensors remain device-resident handles inside the resulting struct or struct array.

Can I pass non-string objects as field names?⌄

No. Field names must be provided as string scalars, character vectors, or single-element string arrays. Passing other types raises an error.

Related Structs functions

fieldnames · getfield · isfield · orderfields · rmfield · setfield · structfun

Open-source implementation

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

  • View the source for struct 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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How struct works
  • Does RunMat run struct on the GPU?
  • GPU memory and residency
  • Examples
  • Creating a simple structure for named fields
  • Building a struct array from paired cell inputs
  • Broadcasting scalars across a struct array
  • Copying an existing structure
  • Building an empty struct array
  • Using struct with coding agents
  • FAQ
  • Related Structs functions
  • Open-source implementation
  • About RunMat