Default Save and Load Process for Objects
MATLAB® follows a default set of steps when serializing (saving) and deserializing (loading) objects. This topic describes the default steps MATLAB uses for classes that have not implemented customized serialization. For more information on customizing serialization, see When to Customize the Serialization Process.
Save and Load Functions
Use save
and load
to serialize and deserialize objects. These are the basic
syntaxes.
save("filename","variablename") load("filename","variablename")
What Information Is Saved?
Serializing objects in MAT-files stores:
The full name of the object class, including any namespace qualifiers.
Values of dynamic properties.
All property default values defined by the class at the time the first object of the class is saved to the MAT-file.
The names and values of all properties, with these exceptions:
Properties are not saved if their current values are the same as the default values specified in the class definition.
Properties are not saved if they are defined as transient, constant, or dependent. For a description of these property attributes, see Property Attributes.
How Is the Property Data Loaded?
When deserializing objects from MAT-files, the load
function
restores the object using these steps:
load
creates a new object.If the class
ConstructOnLoad
attribute is set totrue
,load
calls the class constructor with no arguments. Otherwise,load
does not call the class constructor.load
assigns the saved property values to the object (except in the case of dependent, constant, or transient properties, which are not saved by default). If the class defines set methods for any of the properties, MATLAB calls them. The property values must satisfy any property validation defined by the current definition of the class. For information, see Property Get and Set Methods and Validate Property Values.load
assigns the default values saved in the MAT file to properties whose values were not saved because the properties were set to their default values. These assignments result in calls to property set methods defined by the class.If a property of an object being loaded contains an object, then
load
creates a new object of the same class and assigns it to the property.If one or more properties of the object were renamed or removed from the class definition since the object was serialized, the
load
function reconstitutes the object without those changed properties.If the validation is changed for one or more properties and the saved values are no longer valid, MATLAB substitutes the currently defined default value for that property.