Saving and Loading Objects from Class Hierarchies

Saving and Loading Subclass Objects

When you modify the save operation of an object that is part of a class hierarchy, you must be sure that all classes in the hierarchy perform the correct operations in the save and load process. If the most specific class of an object does not define a loadobj or saveobj method, this class can inherit loadobj or saveobj methods from a superclass.

If any class in the hierarchy defines special save and load behavior:

  • Define saveobj for all classes in the hierarchy.

  • Call superclass saveobj methods from the subclass saveobj method because the save function calls only one saveobj method.

  • If saveobj returns a struct instead of the object, then the subclass can implement a loadobj method to reconstruct the object.

  • The subclass loadobj method can call the superclass loadobj, or other methods as required, to assign values to their properties.

Reconstructing the Subclass Object from a Saved Struct

Suppose you want to save a subclass object by first converting its property data to a struct in the class's saveobj method and then reconstruct the object when loaded using its loadobj method. This action requires that:

  • Superclasses implement saveobj methods to save their property data in the struct.

  • The subclass saveobj method calls each superclass saveobj method and then returns the completed struct to the save function, which writes the struct to the MAT-file.

  • The subclass loadobj method creates a subclass object and then calls superclass methods to assign their property values in the subclass object.

  • The subclass loadobj method returns the reconstructed object to the load function, which loads the object into the workspace.

The following superclass (MySuper) and subclass (MySub) definitions show how to code these methods. The MySuper class defines a loadobj method to enable an object of this class to be loaded directly. The subclass loadobj method calls a method named reload after it constructs the subclass object. reload first calls the superclass reload method to assign superclass property values and then assigns the subclass property value.

classdef MySuper
% Superclass definition
   properties 
      X
      Y
   end
   methods 
      function S = saveobj(obj)
      % Save property values in struct
      % Return struct for save function to write to MAT-file
         S.PointX = obj.X;
         S.PointY = obj.Y;
      end
      function obj = reload(obj,S)
      % Method used to assign values from struct to properties
      % Called by loadobj and subclass
         obj.X = S.PointX;
         obj.Y = S.PointY;
      end
   end
   methods (Static)
      function obj = loadobj(S)
      % Constructs a MySuper object
      % loadobj used when a superclass object is saved directly
      % Calls reload to assign property values retrived from struct
      % loadobj must be Static so it can be called without object
         obj = MySuper;
         obj = reload(obj,S);
      end
   end
end

Your subclass implements saveobj and loadobj methods that call superclass methods.

classdef MySub < MySuper 
% Subclass definition
   properties 
      Z
   end
   methods 
      function S = saveobj(obj)
      % Call superclass saveobj
      % Save property values in struct
         S = saveobj@MySuper(obj);
         S.PointZ = obj.Z;
      end
      function obj = reload(obj,S)
      % Call superclass reload method
      % Assign subclass property value
      % Called by loadobj
         obj = reload@MySuper(obj,S);
         obj.Z = S.PointZ;
      end
   end
   methods (Static)
      function obj = loadobj(S)
      % Create object of MySub class
      % Assign property value retrived from struct
      % loadobj must be Static so it can be called without object
         obj = MySub;
         obj = reload(obj,S);
      end
   end
end
Was this topic helpful?