Hi, im having some trouble overloading horzcat/vertcat for my class with 2 properties:
classdef myClass properties a b end methods function obj = myClass(u,v) obj.a = u; obj.b = v; end function out = horzcat(varargin) ... end endend
As a result of the concatenation i want to achieve just the concatenations of the two proprieties, for example concatenating those objects:
x = myClass(2,3);y = myClass(4,5);ConcArray = [x,y];
i would like to have as a result a single object with as properties the concatenation of each property. Basically having the equivalent of:
EquivalentObj = myClass([2,4],[3,5])
I came up with this solution:
function out = horzcat(varargin) n = length(varargin); aProp = []; bProp = []; for i = 1 : n var = varargin{i}; aProp = [aProp var.a]; bProp = [bProp var.b]; end out = myClass(aProp,bProp);end
but it looks really bad since i couldn't preallocate the concatenating arrays, im using a for loop and in each loop im using standard horzcat recursively.
Any ideas on how to improve that ? Thank you in advance.
Best Answer