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

NameTypeRequiredDefaultDescription
AAnyYesTarget of the indexing expression.
SAnyYesSubstruct-compatible indexing descriptor.
indexingContextAnyYesIndexingContext enum object or compatible text token.

Returns

NameTypeDescription
nNumericScalarNumber of expected subsref outputs or subsasgn inputs.

Errors

IdentifierWhenMessage
RunMat:numArgumentsFromSubscript:InvalidArgumentInputs do not match the documented target, substruct, and indexing-context form.numArgumentsFromSubscript: invalid argument
RunMat:numArgumentsFromSubscript:InvalidSubstructThe substruct-compatible descriptor is missing type/subs fields or has invalid values.numArgumentsFromSubscript: invalid substruct
RunMat:numArgumentsFromSubscript:InvalidContextThe 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.type accepts "()", ".", or "{}" as a string scalar or row character vector.
  • S.subs is 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 type and subs fields.
  • Object and handle-object targets dispatch to the receiver class's numArgumentsFromSubscript method 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 .Assignment are registered lazily when used through classref, and the legacy matlab.mixin.util.IndexingContext class 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 = 2

Use 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 = 3

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

Indexing

subsasgn · subsref

Serialization

loadobj · saveobj

Open-source implementation

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