Mario
Mario

Reputation: 274

Copy MATLAB object of a handle class

I've implemented a handle class in MATLAB, and I've tried to copy an object of the class by calling the following two functions:

objByteArray = getByteStreamFromArray(obj);
newObj = getArrayFromByteStream(objByteArray);

But from time to time I get the following error:

Error using getArrayFromByteStream Unable to read data stream because the data contains a bad version or endian-key

Is there another way to copy an object of a handle class?

Upvotes: 3

Views: 2728

Answers (1)

Sam Roberts
Sam Roberts

Reputation: 24127

Since you're working with a handle class, you can inherit your class from matlab.mixin.Copyable, which will give your class a customizable copy method.

By default, the copy method will implement a shallow copy of the class properties (i.e. if the class properties are themselves handle classes, the copies will be references to the original properties), but you can customize the copy operation to implement a deep copy (i.e. a copy operation is performed on the class properties as well).

Documentation for matlab.mixin.Copyable.

Upvotes: 2

Related Questions