Main Content

System object를 저장하고 불러오기

이 예제에서는 System object™를 불러오고 저장하는 방법을 보여줍니다.

System object와 자식 객체 저장하기

사용자가 System object를 저장할 때 퍼블릭 속성 외의 다른 특성도 저장하도록 saveObjectImpl 메서드를 정의합니다. 이 메서드 내에서 디폴트 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와 자식 객체 불러오기

이전에 저장한 System object를 불러오도록 loadObjectImpl 메서드를 정의합니다. 이 메서드 내에서 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

참고 항목

|