MATLAB: How to split a class into two classes with the same name

classMATLAB

I am trying to create two arrays of the same size for a class.
I define a "material" class and assign a material properties "rho","cp" and "lambda". I do calculations with these properties at 10 different locations and put the output in a 5×2 matrix. Now I want to perform these same calculations for the same "material" object, but I want to assign different values for the defined properties to perform the calculations with.
Is there an easy way to implement this or is it recommended to create a new "material" object to accomplish this?

Best Answer

Note: there is a big semantic difference between a class, a definition of properties and methods, and objects, the instances of the class where the properties have set values.
Your title speaks of duplicating classes whereas it sounds like you want to duplicate objects.
How to duplicate instances of a class depends on a few things.
  • If the class is a value class, it's trivial:
obj2 = obj1;
will create a copy. You can then modify the properties of obj2 without affecting obj1.
  • If the class is a handle class, then unless you've taken particular steps there's no easy way.
The easiest way to enable copy of handle class objects is by superclassing the class from matlab.mixin.copyable. If the properties of the class are all simple non-handle object that's all that is required, and you can then make copies with:
obj2 = copy(obj1); %or obj2 = obj1.copy
Otherwise, you would have to create your own copy method, which I wouldn't recommend as it won't be as robust as matlab.mixin.copyable.
Failing that, you've left to copying the properties yourself one by one or saving the object to a mat file then loading it a new variable.