Modify the Save and Load Process
When to Modify the Save and Load Process
The primary reason for modifying the save and load process is to support backward and forward compatibility of classes. Consider modifying the save and load process when you:
Rename a class
Remove properties
Define a circular reference of handle objects where initialization order is important
Must call the constructor with arguments and, therefore, cannot use
ConstructOnLoad
How to Modify the Save and Load Process
The most versatile technique for modifying the save and load process is to implement loadobj
, and if necessary, saveobj
methods for your class. MATLAB® executes these methods when you call save
or load
on an object of the class.
The save
function calls your class saveobj
method before performing the save operation. The save
function then saves the value returned by the saveobj
method. You can use saveobj
to return a modified object or a struct
that contains property values.
load
calls your class loadobj
method after loading the object. The load
function loads the value returned by the loadobj
method into the workspace. A loadobj
method can modify the object being loaded or can reconstruct an object from the data saved by the class saveobj
method.
Implementing saveobj
and loadobj
Methods
Implement a saveobj
method that modifies the object being saved, then implement a loadobj
method to return the object to the correct state when loading it.
Implement the loadobj
method as a Static
method because MATLAB can call the loadobj
method with a struct
instead of an object of the class.
Implement the saveobj
method as an ordinary method (that is, calling it requires an instance of the class).
MATLAB saves the object class name so that load
can determine which loadobj
method to call in cases where your saveobj
method saves only the object data in a structure. Therefore, the class must be accessible to MATLAB when you load the object.
Use a loadobj
method when:
The class definition has changed since the object was saved, requiring you to modify the object before loading.
A
saveobj
method modified the object during the save operation, possibly saving data in astruct
. Implement theloadobj
method to reconstruct the object from the output ofsaveobj
.
Additional Considerations
When you decide to modify the default save and load process, keep the following points in mind:
If loading any property value from the MAT-file produces an error,
load
passes astruct
toloadobj
. Thestruct
field names correspond to the property names extracted from the file.loadobj
must always be able to accept astruct
as input and return an object, even if there is nosaveobj
orsaveobj
does not return astruct
.If
saveobj
returns astruct
, thenload
always passes thatstruct
toloadobj
.Subclass objects inherit superclass
loadobj
andsaveobj
methods. Therefore, if you do not implement aloadobj
orsaveobj
method in the subclass, MATLAB calls only the inherited methods.If a superclass implements a
loadobj
orsaveobj
method, then a subclass can also implement aloadobj
orsaveobj
method that calls the superclass methods. For more information, see Save and Load Objects from Class Hierarchies.The
load
function does not call the constructor by default. For more information, see Initialize Objects When Loading.Customizing
loadobj
andsaveobj
is not supported for enum classes.