Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

保存和加载 System object

此示例说明如何加载和保存 System object™。

保存 System object 和子对象

定义 saveObjectImpl 方法,以指定当用户保存 System object 时不仅仅应保存公共属性。在此方法内,使用默认的 saveObjectImpl@matlab.System 将公共属性保存到结构体 s。使用 saveObject 方法保存子对象。保存受保护的属性和从属属性,最后,如果该对象已被调用并且未释放,则保存对象状态。

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

加载 System object 和子对象

定义 loadObjectImpl 方法以加载以前保存的 System object。在此方法内,使用 loadObject 加载子级 System object、加载受保护的属性和私有属性、加载状态(如果对象被调用且未释放),然后使用基类中的 loadObjectImpl 加载公共属性。

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

包含保存和加载的完整类定义文件

Counter 类定义文件使用 count 属性来设置对象。此计数器在 MySaveLoader 类定义文件中用来计算子对象的数目。

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 = 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 called and not released
      if isLocked(obj)
        s.state = obj.state;
      end
    end
    
    function loadObjectImpl(obj,s,isInUse)
      % 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 is in use
      if isInUse
        obj.state = s.state;
      end

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

另请参阅

|