Save and Load Process for Objects
Save and Load Objects
load to store and reload objects:
save filename object load filename object
What Information Is Saved?
Saving objects in MAT-files saves:
The full name of the object class, including any package 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 the following 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 their
Dependentattributes set to
For a description of property attributes, see Property Attributes.
To save graphics objects, see
How Is the Property Data Loaded?
When loading objects from MAT-files, the
load function restores the object.
loadcreates a new object.
If the class
ConstructOnLoadattribute is set to
loadcalls the class constructor with no arguments. Otherwise,
loaddoes not call the class constructor.
loadassigns the saved property values to the object properties. These assigned values are subjected to any property validation defined by the class. Then any property set methods defined by the class are called, (except in the case of
Transientproperties, which are not saved or loaded).
loadassigns the default values saved in the MAT-file to properties whose values were not saved because the properties were set to the default values when saved. 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
loadcreates a new object of the same class and assigns it to the property. If the object contained in the property is a handle object, then the property contains a new handle object of the same class.
MATLAB® calls property set methods to ensure that property values are still valid in cases where the class definition has changed.
For information, see Property Get and Set Methods and Validate Property Values.
Errors During Load
If a new version of a class removes, renames, or changes the validation for a property,
load can generate an error when attempting to set the altered or deleted property.
When an error occurs while an object is being loaded from a file, MATLAB does one of the following:
struct passed to the
loadobj method, the field names correspond to the property names. The field values are the saved values for the corresponding properties.
If the saved object derives from multiple superclasses that have private properties with same name, the
struct contains only the property value of the most direct superclass.
For information on how to implement
loadobj methods, see Modify the Save and Load Process.
Changes to Property Validation
If a class definition changes property validation such that loaded property values are no longer valid, MATLAB substitutes the currently defined default value for that property. The class can define a
loadobj method or converter methods to provide compatibility among class versions.
For information on property validation, see Validate Property Values