numArgumentsFromSubscript — Return the expected argument count for MATLAB-style indexing described by a substruct.
numArgumentsFromSubscript(A, S, indexingContext) returns the number of expected outputs from subsref or expected inputs to subsasgn for an indexing expression. S is a substruct-compatible descriptor with type and subs fields, and indexingContext is Statement, Expression, or Assignment through matlab.indexing.IndexingContext or the legacy matlab.mixin.util.IndexingContext name.
Syntax
n = numArgumentsFromSubscript(A, S, indexingContext)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Target of the indexing expression. |
S | Any | Yes | — | Substruct-compatible indexing descriptor. |
indexingContext | Any | Yes | — | IndexingContext enum object or compatible text token. |
Returns
| Name | Type | Description |
|---|---|---|
n | NumericScalar | Number of expected subsref outputs or subsasgn inputs. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:numArgumentsFromSubscript:InvalidArgument | Inputs do not match the documented target, substruct, and indexing-context form. | numArgumentsFromSubscript: invalid argument |
RunMat:numArgumentsFromSubscript:InvalidSubstruct | The substruct-compatible descriptor is missing type/subs fields or has invalid values. | numArgumentsFromSubscript: invalid substruct |
RunMat:numArgumentsFromSubscript:InvalidContext | The indexing context is not Statement, Expression, or Assignment. | numArgumentsFromSubscript: invalid indexing context |
How numArgumentsFromSubscript works
- The public signature is
n = numArgumentsFromSubscript(A, S, indexingContext). S.typeaccepts"()",".", or"{}"as a string scalar or row character vector.S.subsis text for dot indexing and a cell array of index vectors for parentheses or brace indexing.- Scalar structs describe one indexing level. Compound substruct arrays are accepted through RunMat's struct-array cell representation, where each cell element is a scalar struct with
typeandsubsfields. - Object and handle-object targets dispatch to the receiver class's
numArgumentsFromSubscriptmethod when it is registered. If no overload exists, RunMat falls back to default array behavior. - For ordinary arrays, parentheses and dot indexing report one argument. Brace indexing reports the number of selected elements, including validated numeric vector selectors, logical selectors,
':', and multidimensional selector products. matlab.indexing.IndexingContext.Statement,.Expression, and.Assignmentare registered lazily when used throughclassref, and the legacymatlab.mixin.util.IndexingContextclass name resolves to equivalent enum members.- RunMat also accepts compatible text tokens such as
'Statement'and'matlab.indexing.IndexingContext.Expression'for integration with code that constructs context values dynamically. - Invalid substructs and unsupported context values raise stable
RunMat:numArgumentsFromSubscript:*identifiers.
Does RunMat run numArgumentsFromSubscript on the GPU?
For gpuArray targets, RunMat uses GpuTensorHandle.shape to count colon-selected brace outputs.
For GPU-resident numeric index vectors, RunMat returns RunMat:numArgumentsFromSubscript:InvalidSubstruct rather than gathering selector values.
GPU memory and residency
numArgumentsFromSubscript is host-side metadata logic. It does not launch provider kernels, gather target buffers, or allocate GPU outputs.
Examples
Count outputs from brace indexing
C = {"one", 2, "three"};
S = struct();
S.type = '{}';
S.subs = {[1 2]};
n = numArgumentsFromSubscript(C, S, 'Statement')Expected output:
n = 2Use the IndexingContext enum
C = {"one", 2, "three"};
S = struct();
S.type = '{}';
S.subs = {':'};
ctx = classref('matlab.indexing.IndexingContext').Statement;
n = numArgumentsFromSubscript(C, S, ctx)Expected output:
n = 3Using numArgumentsFromSubscript with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how numArgumentsFromSubscript changes the result.
Run a small numArgumentsFromSubscript example, explain the result, then change one input and compare the output.
FAQ
Does RunMat implement the substruct helper itself?⌄
Not in this slice. numArgumentsFromSubscript accepts the same descriptor shape, so user code can build S with struct and field assignment.
Can user classes override the result?⌄
Yes. For object and handle-object targets, RunMat dispatches to a registered Class.numArgumentsFromSubscript method before applying the default array behavior.
Does this gather gpuArray data?⌄
No. The default path only reads target shape metadata from GpuTensorHandle values when counting ':'. GPU-resident index vectors are rejected because their values cannot be validated without gathering.
Related Oop functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how numArgumentsFromSubscript is executed, line by line, in Rust.
- View the source for numArgumentsFromSubscript 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.