How does numArgumen​tsFromSubs​cript work?

As an exercise, I am trying to overload the subsref method in my user-defined class myclass to support the assignment expression,
[obj.X{:}] = deal(1,2,3)
The attached code succeeds in doing so, but as the tracing output shows, numArgumentsFromSubscript is called multiple times with identical inputs before subsasgn is ever reached.
obj=myclass();
[obj.X{:}] = deal(1,2,3)
numArgumentsFromSubscript with n=3 numArgumentsFromSubscript with n=3 Executing subsasgn
obj =
myclass with properties: X: {[1] [2] [3]}
Why is the second invocation of numArgumentsFromSubscript necessary and, more generally, what are the formal rules for when numArgumentsFromSubscript is called relative to subsref/subsasgn?
classdef myclass
properties
X (1,3) cell
end
methods
function obj=subsasgn(obj,S,varargin)
assert(strcmp(S(1).type,'.') && ...
strcmp(S(1).subs,'X'), 'Unrecognized syntax')
obj.X=builtin('subsasgn',obj.X,S(2:end),varargin{:});
disp("Executing subsasgn") %tracing
end
function n=numArgumentsFromSubscript(obj,S,indcon)
import matlab.indexing.IndexingContext
N=numel(S);
if N>1 && isequal(S(2).subs,{':'}) && indcon==IndexingContext.Assignment
n = numel(obj.X);
else
n = 1;
end
disp("numArgumentsFromSubscript with n="+n) %tracing
end
end
end

5 comentarios

dpb
dpb el 20 de Jun. de 2026 a las 19:40
Just for grins I looked at the m-code for the table incarnation and it internally sets a recursion level of two...
...
recurseAtLevel = 2;
% Perform one level of indexing,
% then forward result to builtin numArgumentsFromSubscript
Raag
Raag el 26 de Jun. de 2026 a las 12:02
Hi,
It is my understanding that you are overloading 'subsref' and observing that 'numArgumentsFromSubscript' is invoked multiple times before subsref is executed.
The interaction between 'subsref', 'subsasgn', and 'numArgumentsFromSubscript' is part of MATLAB’s indexing evaluation process, where MATLAB may determine the number of inputs/outputs for an indexing expression depending on the context of use.
You can refer to the following documentation for more information:
Matt J
Matt J el 26 de Jun. de 2026 a las 20:41
You can refer to the following documentation for more information:
Nope. There is nothing at those links that is not already captured in my post.
Raag
Raag el 1 de Jul. de 2026 a las 10:21
Hey Matt,
I believe the double call is a consequence of MATLAB's two-pass evaluation of bracketed assignment expressions.
In the first pass, the interpreter determines how many slots '[obj.X{:}]' expands to, validating that the expression is well-formed. In the second pass, it binds 'nargout' for the right-hand side before executing it.
Both passes independently query 'numArgumentsFromSubscript' since the engine treats them as separate questions and does not cache the result, as the method is user-defined and assumed potentially state-dependent.
Only after both passes succeed does the engine commit to calling 'subsasgn'.
In the second pass, it binds 'nargout' for the right-hand side before executing it.
It seems to me that nargout is determined by the first pass. That is why, below, deal() is able to issue an nargout error after numArgsFromSubscript has been called only once.
obj=myclass;
[obj.X{:}]=deal(1,2,3,4)
numArgumentsFromSubscript with n=3
Error using deal (line 37)
The number of outputs should match the number of inputs.
So, it is still unclear what the 2nd pass is doing.

Iniciar sesión para comentar.

 Respuesta aceptada

James Lebak
James Lebak hace alrededor de 4 horas

1 voto

I believe this is a bug in the execution of overloaded indexing. We will fix this in a future version.
The intent is for numArgumentsFromSubscript to be called in this situation to establish how many inputs subsasgn should expect. We should be able to access this information in the subsasgn call, but we currently are not doing so, and so are explicitly making this call twice. Thanks for pointing this out.

Más respuestas (0)

Categorías

Más información sobre Customize Object Indexing en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 19 de Jun. de 2026 a las 23:07

Respondida:

hace alrededor de 4 horas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by