Skip to Main Content Skip to Search
Product Documentation

Understanding the Save and Load Process

The Default Save and Load Process

Use save and load to store objects:

save filename object
load filename object

What Information Is Saved

Saving objects in MAT-files saves:

Loading Property Data

When loading objects from MAT-files the load function:

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.

Errors During Load

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.

Saving and Loading Deleted Handle Objects

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

See the handle class delete method and the clear command for more information on these operations.

saveobj and loadobj

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.

When to Modify Object Saving and Loading

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.

Why Implement saveobj and loadobj

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:

Information to Consider

If you decide to modify the default save and load process, keep the following points in mind:

See Tips for Saving and Loading for guidelines on saving and loading objects.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS