This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Save and Load Process for Objects

Save and Load Objects

Use save and 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

  • The names and values of all properties, except properties that have their Transient, Constant, or Dependent attributes set to true

For a description of property attributes, see Specify Property Attributes

To save graphics objects, see savefig.

    Note:   Do not use the pack command with objects that define events and listeners. The pack command causes the destructor of any listeners defined for the objects in the workspace. For information on restoring listeners when saving objects, see Restore Listeners.

How Is the Property Data Loaded?

When loading objects from MAT-files, load restores the object. The load function:

  • Creates a new object.

  • Calls the class constructor with no arguments only if the class ConstructOnLoad attribute is set to true. Otherwise, load does not call the class constructor.

  • Assigns the saved property values to the object properties. These assignments result in calls to property set methods defined by the class (except in the case of Dependent or Transient properties, which are not saved or loaded).

You can use property set methods to ensure that property values are still valid in cases where the class definition has changed.

For information on property set methods, see Property Set Methods.

Errors During Load

If a new version of a class removes or renames 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:

  • If the class defines a loadobj method, MATLAB returns the saved values to the loadobj method in a struct.

  • If the class does not define a loadobj method, MATLAB silently ignores the errors. The load function reconstitutes the object with property values that do not produce an error.

In the 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 saveobj and loadobj methods, see Modify the Save and Load Process.

Was this topic helpful?