When to Customize the Serialization Process
Supporting backward and forward compatibility of classes is the primary reason to customize the serialization (saving) and deserialization (loading) process for objects. Customize the serialization process when you change a class definition, including but not limited to:
Renaming properties
Adding or removing properties
Changing validation for properties
How to Modify the Serialization Process
MATLAB® includes two ways to customize the serialization process:
Inherit from
matlab.mixin.CustomElementSerialization. (since R2024b)
The recommended process for customizing serialization is to inherit from
matlab.mixin.CustomElementSerialization. This mixin, combined with the
functionality of matlab.serialization.ElementSerializationContent and matlab.serialization.SerializationContext, enables you to control what
information is serialized and deserialized. This control supports robust backward and
forward compatibility between different versions of the same class.
matlab.serialization.SerializationContext can be useful when
serializing to destinations other than MAT files.
matlab.mixin.CustomElementSerialization also supports the addition of
dynamic properties during serialization, which loadobj and
saveobj do not.
Use loadobj and saveobj when:
You have an existing implementation of
saveobjthat serializes data in a type other than an object or astruct, and yourloadobjimplementation can only deserialize data from that particular type.You have an existing implementation of
loadobjthat can only handle structure inputs. Revising your class to usematlab.mixin.CustomElementSerializationcan break forward compatibility when the revised class saves data as an object becauseloadobjin the older class definition might not be able to load all properties. See Transitioning Serialization and Deserialization Processes to matlab.mixin.CustomElementSerialization for more information.You are using a MATLAB release older than R2024b.
See Also
matlab.mixin.CustomElementSerialization | matlab.serialization.ElementSerializationContent | matlab.serialization.SerializationContext | loadobj | saveobj