Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

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:

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:

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
  


Recommended Products

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

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