| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Use save and load to store objects:
save filename object load filename object
Saving objects in MAT-files saves:
The names and current values of all properties, except those that have their Transient, Constant, or Dependent attributes set to true.
The values of dynamic properties.
The full name of the object's class, including any package qualifiers.
See Specifying Property Attributes for a description of property attributes.
When loading objects from MAT-files the load function:
Creates a new object and assigns the values that are stored in the MAT-file.
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, which calls any defined 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.
The save and load functions call your class's saveobj and loadobj methods, respectively, if your class defines these methods. You use these methods to customize the save and load process.
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 following sections describe when and how to modify the process MATLAB uses to save and load objects. You modify this process by implementing saveobj and loadobj methods for your class.
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.
![]() | Saving and Loading Objects | Modifying the Save and Load Process | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |