MATLAB: Overloading of subsref partially failing

class subsref overload indexingoop

Hi,
I want to create an array class of generic dimensions. So I wanted to overload the subsref indexing. Worked mostly on first sight, now encounter strange behavior, as shown in this minimal example:
Main.m:
ThisArray = MyArray;
ThisArray(:) % <---- This works fine

ThisArray.ChangeArray;
ThisArray(:) % <---- This works fine
MyArray.m:
classdef MyArray < handle
properties
MainArray
end
methods
function obj = MyArray
obj.MainArray = [1 2; 3 4];
end % function MyArray
function sref = subsref(obj,s)
switch s(1).type
case '()'
sref = obj.MainArray(s(1).subs{:});
otherwise
sref = builtin('subsref',obj,s);
end % switch
end % function subsref
function obj = ChangeArray(obj)
obj.MainArray(1) = obj.MainArray(1)+1;
obj(:) % <------ This one only displays object

NewArray = MyArray;
NewArray(:) % <------ This one only displays object
end % function ChangeArray
end
end
Running this from Main works fine in Main both marked occurrences output the MainArray as wanted. But when I call ChangeArray the subsref overload does not work anymore. Both tries to use the overloaded indexing just outputs the object, indicating the my overloaded subsref is not reacting and (:) is just ignored. Breakpoint on switch in subsref shows no entering of this function on both problematic occasions.
Why can't I use those indices within this object method.
P.S.: I liked the handle inheritance for my class, but removing it did not appear to make a difference.
Thanks for any help.

Best Answer

SUBSREF is not triggered by index expressions inside a classdef file. Inside the classdef, you have to call subsref explicitly if you want it to execute:
function obj = ChangeArray(obj)
obj.MainArray(1) = obj.MainArray(1)+1;
S=struct('type','()','subs',{':'});
subsref(obj,S)
NewArray = MyArray;
subsref(NewArray,S)
end % function ChangeArray
This is meant to allow you to use index expressions inside the subsref method without them triggering an infinite chain of recursive calls to the subsref method itself.