Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Save System Object

This example shows how to save a System object™.

Save System Object and Child Object

Define a saveObjectImpl method to specify that more than just public properties should be saved when the user saves a System object. Within this method, use the default saveObjectImpl@matlab.System to save public properties to the struct, s. Use the saveObject method to save child objects. Save protected and dependent properties, and finally, if the object is locked, save the object's state.

methods(Access=protected)
   function s = saveObjectImpl(obj)      
      s = saveObjectImpl@matlab.System(obj);
      s.child = matlab.System.saveObject(obj.child);
      s.protected = obj.protected;
      s.pdependentprop = obj.pdependentprop;
      if isLocked(obj)
         s.state = obj.state;
      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, varargin)
      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

See Also

|

Related Examples

Was this topic helpful?