struct — Create scalar structs or struct arrays from name/value pairs.
S = struct(...) creates scalar structs or struct arrays by pairing field names with values. The inputs can be simple name/value pairs, existing structs, or cell arrays whose elements are expanded into struct array entries.
How struct works in RunMat
- Field names must satisfy the MATLAB
isvarnamerules: they start with a letter or underscore and contain only letters, digits, or underscores. - 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, whilestruct([])yields a0×0struct 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.
- Passing an existing struct or struct array (
struct(S)) creates a deep copy; the original data is untouched.
How struct runs 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
42Building 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 8Building an empty struct array
s = struct([]);
disp(size(s))Expected output:
0 0FAQ
Do field names have to be valid identifiers?
Yes. RunMat mirrors MATLAB and requires names to satisfy isvarname. Names must begin with a letter or underscore and may contain letters, digits, and underscores.
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 functions to explore
These functions work well alongside struct. Each page has runnable examples you can try in the browser.
load, whos, gpuArray, gather, fieldnames, getfield, isfield, orderfields, rmfield, setfield
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how struct works, line by line, in Rust.
- View struct.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.