MATLAB: Fill object properties from another class object

oopsubclass

Hi there,
I load an object of a class 'xdTest' from an API and would like to add to this object one more property.
I first thought of creating a class 'Test' with my extra property inheriting the subclass 'xdTest' properties like :
classdef Test < xdTest
properties
myExtraProp
end
end
But the thing is that I can't manage to create a Test object and fill its inherited properties with an existing xdTest object.
Any idea of a way to fill or merge its inherited properties without having to do it for every property in a constructor method?
Thank you so much!

Best Answer

Assuming that the base class does not have a copy constructor (a constructor that simply takes another object of the base class) I think the only way is indeed to copy all the properties. And that's assuming that the properties have public or protected set access.
You can automate that though:
classdef Test < xdTest
properties
myExtraProp
end
methods
function this = Test(xdTestobj, extraprop)
mco = ?xdTest;
for prop = mco.PropertyList'
if ismember(prop.SetAccess, {'public', 'protected'})
this.(prop.Name) = xdTestobj(prop.Name);
else
warning('could not copy property %s', prop.Name);
end
end
this.myExtraProp = extraprop;
end
end
end