MATLAB Object-Oriented Programming

(Joyce) #1

Modify the Save and Load Process


In this section...
“When to Modify the Save and Load Process” on page 13-16
“How to Modify the Save and Load Process” on page 13-16
“Implementing saveobj and loadobj Methods” on page 13-17
“Additional Considerations” on page 13-17

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.

13 Saving and Loading Objects

Free download pdf