Overloading of subsref partially failing

6 Ansichten (letzte 30 Tage)
Sven
Sven am 27 Okt. 2016
Kommentiert: Sven am 28 Okt. 2016
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.

Akzeptierte Antwort

Matt J
Matt J am 27 Okt. 2016
Bearbeitet: Matt J am 27 Okt. 2016
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.
  1 Kommentar
Sven
Sven am 28 Okt. 2016
Thanks for the answer. Makes sense to avoid that possible recursion. Seems to be obvious after you said it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 27 Okt. 2016
See the "Built-In subsref and subsasgn Called in Methods" section on this documentation page.
If you want to invoke an overloaded subsref or subsasgn method inside a method you must do so explicitly by calling subsref(...) or subsasgn(...).
You cannot invoke those overloaded indexing methods implicitly by indexing into the object using parentheses like obj(1), curly braces like obj{2}, or dot referencing like obj.prop1.

Kategorien

Mehr zu Customize Object Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by