Load System Object

This example shows how to load a System object™.

Load System Object and Child Object

Define a loadObjectImpl method to load a previously saved System object. Within this method, use the matlab.System.loadObject to assign the child object struct data to the associated object property. Assign protected and dependent property data to the associated object properties. If the object was locked when it was saved, assign the object's state to the associated property. Load the saved public properties with the loadObjectImpl method.

methods (Access = protected)
    function loadObjectImpl(obj,s,wasLocked)
      obj.child = matlab.System.loadObject(s.child);
      obj.protected = s.protected;
      obj.pdependentprop = s.pdependentprop;
      if wasLocked
        obj.state = s.state;
      end
      loadObjectImpl@matlab.System(obj,s,wasLocked);
    end    
   end
end

Complete Class Definition File with Save and Load

classdef MySaveLoader < matlab.System
 
  properties (Access = private)
    child
    pdependentprop
  end
  
  properties (Access = protected)
    protected = rand;
  end
  
  properties (DiscreteState = true)
    state
  end
  
  properties (Dependent)
    dependentprop
  end
      
  methods
    function obj = MySaveLoader(varargin)
      obj@matlab.System();
      setProperties(obj,nargin,varargin{:});
    end
  end
  
  methods (Access = protected)
    function setupImpl(obj)
      obj.state = 42;
    end
    
    function out = stepImpl(obj,in)
      obj.state = in;
      out = obj.state;
    end
  end
  
  
  % Serialization
  methods (Access = protected)
    function s = saveObjectImpl(obj)      
      % Call the base class method
      s = saveObjectImpl@matlab.System(obj);
      
      % Save the child System objects
      s.child = matlab.System.saveObject(obj.child);
      
      % Save the protected & private properties
      s.protected = obj.protected;
      s.pdependentprop = obj.pdependentprop;
      
      % Save the state only if object locked
      if isLocked(obj)
        s.state = obj.state;
      end
    end
    
    function loadObjectImpl(obj,s,wasLocked)
      % Load child System objects
      obj.child = matlab.System.loadObject(s.child);

      % Load protected and private properties
      obj.protected = s.protected;
      obj.pdependentprop = s.pdependentprop;
      
      % Load the state only if object locked
      if wasLocked
        obj.state = s.state;
      end

      % Call base class method to load public properties
      loadObjectImpl@matlab.System(obj,s,wasLocked);
    end    
  end
end

Related Examples

Was this topic helpful?