|On this page…|
The save function calls your class's saveobj method before performing the save operation. The save function then saves the value returned by the object's saveobj method. You can use the saveobj method to return a modified object or any other type of variable, such as a struct array.
The load function calls your class's loadobj method after loading the object. The load function loads into the workspace the value returned by the object's loadobj method. If you define a loadobj method you can modify the object being returned or reconstruct an object from the data saved by your saveobj method.
If you implement a saveobj method that modifies the object being saved, implement a loadobj method to return the object to its proper state when reloading it. For example, you might want to store an object's data in a struct array and reconstruct the object when reloaded to manage changes to the class definition.
You must implement the loadobj method as a Static method because loadobj can actually be called with a struct or other data instead of an object of the class. You can implement the saveobj method as an ordinary method (i.e., calling it requires an instance of the class).
MATLAB® saves the object's class name so that load can determine which loadobj method to call, even if your saveobj method saves only the object's data in an array and not the object itself.
Implementing a loadobj method enables you to apply some processing to the object before it is loaded into the workspace. You might need to do this if:
The class definition has changed since the object was saved and you need to modify the object before reloading.
A saveobj method modified the object during the save operation, perhaps saving data in an array, and the loadobj method must reconstruct the object based on the output of saveobj.
In the following example, the loadobj method checks if the object to be loaded has an old, shorter account number and calls a function to return an updated account number if necessary. After updating the object's AccountNumber property, loadobj returns the object to be loaded into the workspace.
methods (Static = true) function obj = loadobj(a) accnb = a.AccountNumber; if length(num2str(accnb)) < 12 a.AccountNumber = updateAccountNumber(accnb); % update object end obj = a; % return the updated object end end
In this case, you do not need to implement a saveobj method. You are using loadobj only to ensure older saved objects are brought up to date before loading.
The Save and Load Applications section provides an example in which loadobj performs specific operations to recreate an object based on the data returned by saveobj during the save operation.
The following sections describe some specific applications involving the saving and loading of objects.
Maintaining Class Compatibility — how to maintain compatibility among progressive versions of an application.
Passing Arguments to Constructors During Load — using loadobj to call the class constructor of an object when you need to pass arguments to the constructor during load.
Saving and Loading Objects from Class Hierarchies — how inherited methods affect saving and loading objects.
Saving and Loading Dynamic Properties — how to handle dynamic properties when saving and loading objects.