How do I enable subscripting in a custom class inhereting uint64
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 subsref
Index 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 can
crash 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!
0 commentaires
Réponse acceptée
Philip Borghesani
le 17 Avr 2013
Modifié(e) : Philip Borghesani
le 17 Avr 2013
What is the purpose of prop? This class is index-able and works as a uint64.
classdef tclass < uint64
methods
function obj = tclass(val)
obj = obj@uint64(val);
end
end
end
Once you add a property indexing into an overloaded base type becomes harder to define. Does you object contain a uint64 array and some other global description or should prop have the same dimensions as the data it holds? what is obj(1)?
One possible overly simplified solution for your class:
classdef tclass < uint64
properties
prop
end
methods
function obj = tclass(val)
obj = obj@uint64(val);
end
function ret = subsref(self, S)
val=uint64(self);
ret = val(S.subs{1});
end
end
end
If you want to use buitin to implement subsref try:
ret = builtin('subsref', uint64(self), S);
0 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!