|On this page…|
save filename object load filename object
Saving objects in MAT-files saves:
The full name of the object's class, including any package qualifiers.
Values of dynamic properties.
The names and current values of all properties, except:
Properties that have their Transient, Constant, or Dependent attributes set to true. See Specify Property Attributes for a description of property attributes.
When loading objects from MAT-files the load function:
Creates a new object.
Calls the class constructor with no arguments only if the class's ConstructOnLoad attribute is set to true.
Assigns the saved values to the object's properties. These assignments results in calls to property set methods defined by the class.
You can use property set methods to ensure property values are still valid in cases where the class definition has changed.
See Property Set Methods for information on property set methods.
It is possible for a default value to cause an error in a property set method (for example, the class definition might have changed). When an error occurs while an object is being loaded from a file, MATLAB® returns the saved values in a struct. The field names correspond to the property names.
In cases where the saved object is derived from multiple superclasses that define private properties having the same name, the struct contains the property value of the most direct superclass only.
If you save a deleted handle, MATLAB load it as a deleted handle. For example:
% Create a handle object >> a = containers.Map('Monday','sunny') isvalid(a) ans = 1 % Delete the handle object >> delete(a) >> isvalid(a) ans = 0 % Save the deleted handle >> save savefile a % Clear the variable a >> clear a % Load a back into the workspace >> load savefile a >> isvalid(a) ans = 0
When you issue a save command, MATLAB first calls your saveobj method and passes the output of saveobj to save. Similarly, when you call load, MATLAB passes the result of loading what you saved to loadobj. loadobj must then return a properly constructed object. Therefore, you must design saveobj and loadobj to work together.
The primary reason for implementing saveobj and loadobj methods is to support backward and forward compatibility of classes. For example, you might have cases where:
The class's properties have changed (just adding a new property does not necessarily require special code because it can be initialized to its default value when loaded).
The order in which properties are initialized is important due to a circular reference to handle objects.
You must call the object's constructor with arguments and, therefore, cannot support a default constructor (no arguments).
If you decide to modify the default save and load process, keep the following points in mind:
If your loadobj method generates an error, MATLAB still loads the objects in whatever state the object was in before the invocation of loadobj.
Subclass objects inherit superclass loadobj and saveobj methods. Therefore, if you do not implement a loadobj or saveobj method in the most specific class, MATLAB calls only the inherited methods.
If a superclass implements a loadobj or saveobj method, then your subclass can also implement a loadobj or saveobj method that calls the superclass methods as necessary. See Saving and Loading Objects from Class Hierarchies for more information.
The load function does not call the default constructor by default. See Calling Constructor When Loading for more information.
If an error occurs while the object is loading from a file, the load function passes your loadobj method as much data as it can successfully load from the file. In case of an error, load passes loadobj a struct whose field names correspond to the property names extracted from the file. See Reconstructing Objects with loadobj for an example of a loadobj method that processes a struct.
See Tips for Saving and Loading for guidelines on saving and loading objects.