Main Content

numArgumentsFromSubscript

Number of arguments from indexing methods

Description

n = numArgumentsFromSubscript(A,S,indexingContext) returns the number of expected inputs to subsasgn or the number of expected outputs from subsref. The target of the indexing expression is A, and the structure S contains the details of the indexing expression. indexingContext is an enumerated value that identifies whether the indexing expression occurs in a statement, expression, or assignment.

example

Examples

collapse all

Create a 1-by-3 cell array.

C = {"one",2,'three'};

Calculate the number of expected outputs from the brace indexing statement C{1:2}. First define the substruct that describes this indexing expression.

S = substruct('{}',{1:2});

The indexing context in C{1:2} is matlab.indexing.IndexingContext.Statement. Calculate the number of expected outputs by calling numArgumentsFromSubscript with the cell array C, the substruct S, and the context.

numArgumentsFromSubscript(C,S,matlab.indexing.IndexingContext.Statement)
ans =

     2

Call the indexing statement directly to verify that it returns two arguments.

C{1:2}
ans = 

    "one"


ans =

     2

Input Arguments

collapse all

Target of indexing expression, specified as an array of any data type.

Indexing argument structure, also referred to as the substruct, specified as a structure array. Each structure has two fields:

  • type — Type of indexing expression, specified as "()", ".", or "{}" to indicate parentheses, dot, or brace indexing, respectively. The value can be a character vector or string scalar.

  • subs — Subscript values, specified as a character vector or string for dot indexing or a cell array of index vectors for parentheses or brace indexing.

S is a scalar structure for single indexing expressions, like A{1}, or a structure array for compound indexing expressions. For example, A{1}.field(3:5) has three levels of indexing. For this expression, S is a 1-by-3 structure array:

S(1)

type: '{}'

subs: {[1]}

S(2)

type: '.'

subs: 'field'

S(3)

type: '()'

subs: {[3 4 5]}

Data Types: struct

Context of indexing expression, specified as a matlab.indexing.IndexingContext enumeration object:

  • matlab.indexing.IndexingContext.Statement — The indexing expression is a standalone statement (obj.a).

  • matlab.indexing.IndexingContext.Expression — The indexing expression is part of a statement, such as an argument for a function (func(obj.a)).

  • matlab.indexing.IndexingContext.Assignment — The indexing expression is on the left-hand side of an assignment statement ([obj.a] = x).

    Note

    matlab.indexing.IndexingContext is the latest name for this class. The previous name, matlab.mixin.util.IndexingContext, is still supported and has the same functionality.

Tips

Version History

Introduced in R2015b