My goal was creating an object by the concatenation of other objects. For example let Class1 be a class with a property foo, doing
A = class1()
B = class1()
C = [A B]
could create a class2 object and I would be able to do
C(1) to access A,
C(2) to access B,
C(1).foo to access A.foo .
For doing the concatenation, I overloaded the horzcat, vertcat, cat methods of class1 in which I simply call the constructor of class2.
function obj = horzcat (obj1, varargin)
obj = class2(obj1, varargin{:});
end
In class2 I store the class1 objects in a struct property (let's call it bar). So I had to overload subsref, replacing the '()' access to the obejct with the '{}' access to bar in order enable commands like C(1).foo .
This is the way I thought about implementing class2:
classdef class2 < handle
properties (Access = private)
bar = {};
end
methods (Hidden, Access = {?class1})
function this = class2 (varargin)
this.bar = cell(1, nargin);
for i = 1:nargin
this.bar{i} = varargin{i};
end
end
end
methods
function obj = subsref(this, idx)
switch idx(1).type
case '()'
idx(1).type = '{}';
obj = builtin('subsref', this.bar, idx);
case '{}'
error('{} indexing not supported');
case '.'
error('. indexing not supported');
end
end
end
end
Is this the right way to do it?
The problem is that when I try to access more than one property, it does not work: