Apologies if this is a simple question; I'm new to OOP with Matlab.
I'm using Matlab 8.0.0.783 (R2012b) on a PCWIN64.
I'd like to enable subscripting in a custom class inheriting uint64. For example:
classdef TestClass < uint64 properties prop end methods function obj = TestClass(val) obj = obj@uint64(val); end function ret = subsref(self, S) ret = builtin('subsref', self, S); end end end
Assuming the above is saved to a file TestClass.m in the current path, when I execute the following in the command window
A = TestClass([4, 5, 6]);B = A(1)
the variable B contains the entirety of A, and not just 4 as I would desire. Not surprisingly, if I execute A(2), I receive an "Index exceeds…" error:
Error using subsrefIndex exceeds matrix dimensions.Error in TestClass/subsref (line 13) ret = builtin('subsref', self, S);
In place of the "builtin" line, I have also tried:
subsref@uint64(self, S) % (1)
subsref(self, S) % (2)
self(S.subs{1}) % (3)
(1) returns the "subsref overloading necessary" error:
Cannot use '(' or '{' to index into an object of class 'TestClass' because 'TestClass' defines properties and subclasses 'uint64'.Click here for more information. Error in TestClass/subsref (line 14) ret = subsref@uint64(self, S);
(2) returns the recursion error:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)to change the limit. Be aware that exceeding your available stack space cancrash MATLAB and/or your computer.Error in TestClass/subsref
(3) returns the entire object:
B = TestClass Properties: prop: [] uint64 data: 4 5 6 Methods, Superclasses
None of the errors seem all that surprising. The behavior that is surprising me is that of the "builtin" command. It's as if the interpreter cannot find the subsref method of the bound uint64 object and is falling back by just returning the object itself?
My question is: Why doesn't the "builtin" code work here as I would expect? How would I go about subscripting the bound superclass within the subclass's subsref method?
Thanks in advance for your help!
Best Answer