I prepared this simple classdef.
classdef myclassA properties prop end methods function obj = myclassA(val) obj.prop = val; end function out = horzcat(varargin) disp('horzcat was invoked'); out = builtin('horzcat',varargin{:}); end function out = vertcat(varargin) disp('vertcat was invoked'); out = builtin('vertcat',varargin{:}); end end end
Then I performed concatenation of objects.
>> obj1 = myclassA(1);>> obj2 = myclassA(2);>> [obj1,obj2]horzcat was invokedans = 1x2 myclassA array with properties: prop>> [obj1;obj2]vertcat was invokedans = 2x1 myclassA array with properties: prop
So far so good. However, when I put the same objects into a structure, I've got rather unexpected behaviours of vertcat.
>> S.obj1 = obj1;>> S.pbj2 = obj2;>> [S.obj1,S.obj2]horzcat was invokedans = 1x2 myclassA array with properties: prop >> [S.obj1;S.obj2]horzcat was invokedhorzcat was invokedvertcat was invokedans = 2x1 myclassA array with properties: prop
Here, the two objects were processed by horzcat twice and then by vertcat once. I wonder if this is expected behaviour (I doubt it) or it is a bug.
When I used vertcat explicitly rather than [a;b], I obtained results in a expected way.
>> vertcat(S.obj1,S.obj2)]vertcat was invokedans = 2x1 myclassA array with properties: prop
MATLAB R2015b, on Mac OS X El Capitan
Best Answer