Main Content

saveobj

Customize save process for objects

Description

Note

The matlab.mixin.CustomElementSerialization class is recommended over loadobj and saveobj because the mixin provides greater control over how objects are serialized and deserialized, including the ability to add, delete, and rename properties. (since R2024b)

b = saveobj(a) is called by the save function if the class of a defines a saveobj method. save writes the returned value, b, to a MAT file.

example

Examples

collapse all

The ContactEntry class defines three properties. The class defines a saveobj method that saves those property values in a structure, and a loadobj method that recreates the object from the saved structure.

classdef ContactEntry
   properties
      Name
      Email
      Cell
   end
   methods
      function s = saveobj(obj)
         s.Name = obj.Name;
         s.Email = obj.Email;
         s.Cell = obj.Cell;
      end
   end
   methods (Static)
      function obj = loadobj(s)
         if isstruct(s)
            newObj = ContactEntry;
            newObj.Name = s.Name;
            newObj.Email = s.Email;
            newObj.Cell = s.Cell;
            obj = newObj;
         else
            obj = s;
         end
      end
   end
end

Create an instance of ContactEntry and save it.

a = ContactEntry;
a.Name = "Sandy Trent";
a.Email = "strent@notacompany.com";
a.Cell = "617-555-1212";
save("C:\yourpath\ContactFile.mat","a");

Revise ContactEntry to include a new property that holds a work phone number.

classdef ContactEntry
   properties
      Name
      Email
      Cell
      WorkPhone
   end
   properties (Transient,Hidden)
      SaveInOldFormat = false;
   end
   methods
      function s = saveobj(obj)
         s.Name = obj.Name;
         s.Email = obj.Email;
         s.Cell = obj.Cell;
         if ~obj.SaveInOldFormat
             s.WorkPhone = obj.WorkPhone;
         end
      end
   end
   methods (Static)
      function obj = loadobj(s)
         if isstruct(s)
            newObj = ContactEntry;
            newObj.Name = s.Name;
            newObj.Email = s.Email;
            newObj.Cell = s.Cell;
            if numel(fieldnames(s)) == 4
                newObj.WorkPhone = s.WorkPhone;
            else
                newObj.WorkPhone = "unknown";
            end
            obj = newObj;
         else
            obj = s;
         end
      end
   end
end
To maintain compatibility between the two class versions:

  • The loadobj method has been modified to check for a fourth field in the serialized structure. If there is a fourth field, loadobj sets the WorkPhone property to that value. If not, it sets the WorkPhone property to "unknown".

  • A new transient, hidden property, SaveInOldFormat, enables class users to specify whether they want to serialize an instance of the class in the old format, without the WorkPhone property value. The saveobj method only saves the WorkPhonevalue if SaveInOldFormat is false.

Clear the existing instance a from memory, and load a under the new definition of the class. The revised loadobj method recognizes the saved data as being from the old version of the class, and sets WorkPhone appropriately.

clear a
load("C:\yourpath\ContactFile.mat","a")
a
a = 

  ContactEntry with properties:

         Name: "Sandy Trent"
        Email: "strent@notacompany.com"
         Cell: "617-555-1212"
    WorkPhone: "unknown"
Likewise, you can create a new instance of the class with a value of WorkPhone, and both the saveobj and loadobj methods save and load the value as expected under the new class definition.

Input Arguments

collapse all

Object to serialize.

Output Arguments

collapse all

Data passed to save function by MATLAB®, which can be:

  • An object

  • Property names and current values stored in a structure

Tips

  • Implement your saveobj method to work with scalar objects or structures. When you save an object array, save calls saveobj on each element of the array.

Version History

Introduced before R2006a