Documentation

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.protectedprop = obj.protectedprop;
    s.pdependentprop = obj.pdependentprop;
    if isLocked(obj)
      s.state = obj.state;
    end
  end
end

Complete Class Definition Files with Save and Load

The Counter class definition file sets up an object with a count property. This counter is used in the MySaveLoader class definition file to count the number of child objects.

classdef Counter < matlab.System
  properties(DiscreteState)
    Count
  end
  methods (Access=protected)
    function setupImpl(obj, ~)
      obj.Count = 0;
    end
    function y = stepImpl(obj, u)
      if u > 0
        obj.Count = obj.Count + 1;
      end
      y = obj.Count;
    end
  end
end
classdef MySaveLoader < matlab.System
 
  properties (Access = private)
    child
    pdependentprop = 1
  end
  
  properties (Access = protected)
    protectedprop = rand;
  end
  
  properties (DiscreteState = true)
    state
  end
  
  properties (Dependent)
    dependentprop
  end
      
  methods
    function obj = MySaveLoader(varargin)
      obj@matlab.System();
      setProperties(obj,nargin,varargin{:});
    end
    
    function set.dependentprop(obj, value)
      obj.pdependentprop = min(value, 5);
    end
    
    function value = get.dependentprop(obj)
      value = obj.pdependentprop;
    end
  end
  
  methods (Access = protected)
    function setupImpl(obj)
      obj.state = 42;
      obj.child = Counter;
    end
    function out = stepImpl(obj,in)
      obj.state = in + obj.state + obj.protectedprop + obj.pdependentprop;
      out = step(obj.child, 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.protectedprop = obj.protectedprop;
      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.protectedprop = s.protectedprop;
      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?