Classdef Static method calling builtin subsref instead of classdef subsref
Show older comments
I've got a custom class class called myclass that is a thin wrapper around a double array. So, when constructing arrays of these objects I want the arrays to build up in the private data as a normal double array instead of using the default MATLAB behavior for arrays of classdef objects. That way I don't have to deal with the array data piecemeal in my overloaded methods, but can just use normal MATLAB operators on the private double array in the background. E.g., something like this
classdef myclass
properties (Access = private)
x
end
methods(Static)
function SelfTest
mc1 = myclass(4);
mc2 = myclass(5);
mcarray = [mc1,mc2];
disp(mcarray.getx)
% the following call the builtin subsref instead of myclass subsref ... not what I want
disp(mcarray(1)) % fails and displays the entire data set
disp(mcarray(2)) % fails with an error
end
end
methods
function mc = myclass(value)
if( isa(value,'myclass') )
mc = value;
else
mc.x = double(value); % Any special argument checks would go here
end
end
function value = getx(c)
value = c.x;
end
function disp(c)
disp(c.x);
end
function mc = horzcat(varargin) % horzcat the data into the private property x
c = cell(1,nargin);
for k=1:nargin
v = myclass(varargin{k}); % convert to myclass for special checks, etc.
c{k} = v.getx;
end
mc = myclass(horzcat(c{:})); % all the data goes into one double array
end
function mc = subsref(obj,s)
switch s(1).type
case '.'
mc = builtin('subsref',obj,s);
case '()'
if( length(s) == 1 )
mc = myclass(builtin('subsref',obj.x,s));
else
error('Unsupported indexing expression');
end
otherwise
error('Unsupported indexing expression');
end
end
end
end
As part of the design, I have a Static SelfTest method that tests various features of the class. Everytime I make updates to the class, I will add more tests to this method and run it to see if everything still works. The problem is that this Static method is calling the builtin subsref method instead of myclass subsref method, so it fails. If I put the SelfTest code in a separate file, everything works as expected and the myclass subsref is called. I'm assuming I may have the same problem once I implement subsassign.
How can I get Static methods in myclass to call the myclass subsref method using normal syntax? I tried having the Static method call a non-Static method invoked via a myclass object, but that didn't work either. Having a separate file for this works, but is a bit annoying. E.g., take this file:
function myclass_SelfTest
mc1 = myclass(4);
mc2 = myclass(5);
mcarray = [mc1,mc2];
disp(mcarray.getx)
disp(mcarray(1))
disp(mcarray(2))
end
At the command line the separate file works as expected:
>> myclass_SelfTest
4 5
4
5
But putting that same code into myclass as a Static method doesn't:
>> myclass.SelfTest
4 5
4 5
Index exceeds the number of array elements (1).
Error in myclass.SelfTest (line 15)
disp(mcarray(2))
1 Comment
Paul
12 minutes ago
Hi James,
I don't know why it doesn't work as coded. But I did find that calling the class subsref explicitly from the static method worked in this simple example, e.g.,
disp(subsref(mcarray,struct('type','()','subs',{{2}})));
inside SelfTest gave the expected result, for whatever that's worth.
Answers (0)
Categories
Find more on Customize Object Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!