Comma-separated assignment to a classdef property
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)
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
endAs 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)
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 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)
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 subsasgn, csl, indexing, oop MATLAB Answers — New Questions









