numArgumentsFromSubscript
Method a class can overload to say how many outputs an indexing expression returns.
Category: Programming > Classes
Syntax
n = numArgumentsFromSubscript(obj, s, indexingContext)
Returns the number of values produced when obj is indexed with the chain s in the given context.
Description
When a class overloads subsref or subsasgn, MATLAB must know how many values an expression such as obj.list{:} yields before it can call the overloaded method with the right nargout. The default rule counts the elements an ordinary index would select. A class overrides numArgumentsFromSubscript to apply its own rule, for instance to return 1 for any index into a container.
The language normally calls this method itself, not user code.
Input arguments
obj- The object being indexed.
s- Indexing structure array with fields
typeandsubs, as built bysubstruct. indexingContext- A member of
matlab.indexing.IndexingContext:Statement,ExpressionorAssignment.
Output arguments
n- Number of outputs (or inputs, for an assignment) the expression represents.
Example
A class that indexes into its own list
writelines(["classdef Bag", " properties", " Items = {}", " end", " methods", " function n = numArgumentsFromSubscript(obj, s, ctx)", " n = 1;", " end", " function out = subsref(obj, s)", " out = obj.Items{s(1).subs{1}};", " end", " end", "end"], 'Bag.m');
b = Bag;
b.Items = {10, 20, 30};
b(2)
ans = 20
b(2) calls the overloaded subsref; numArgumentsFromSubscript tells the language it returns exactly one value.
Notes
MatLivre does not provide the matlab.indexing.IndexingContext enumeration, so calling numArgumentsFromSubscript directly always reports an error. A class can still define the method, and indexing of such classes works.