Main Content

System object에서 전역 변수 사용하기

전역 변수는 다른 MATLAB® 함수나 Simulink® 블록에서 액세스할 수 있는 변수입니다.

MATLAB의 System object 전역 변수

MATLAB에서만 사용되는 System object의 경우, 다른 MATLAB 코드에 전역 변수를 정의하는 것과 같은 방식으로 System object™ 클래스 정의 파일에 전역 변수를 정의할 수 있습니다(전역 변수(Global Variable) 참조).

Simulink의 System object 전역 변수

Simulink의 MATLAB System 블록에 사용되는 System object의 경우, 역시 MATLAB에서처럼 전역 변수를 정의할 수 있습니다. 그러나 Simulink에서 전역 변수를 사용하려면 stepImpl, updateImpl 또는 outputImpl에 의해 호출되는 메서드에 전역 변수를 선언한 경우 stepImpl, updateImpl 또는 outputImpl 메서드에 해당 전역 변수를 선언해야 합니다.

MATLAB Function 블록의 경우와 같은 방식으로 MATLAB System 블록에 대한 전역 변수를 설정하고 사용합니다(데이터 저장소 (Simulink)MATLAB Function 블록에서 전역 데이터 사용하기 (Simulink) 참조). 또한 MATLAB Function 블록과 마찬가지로, Simulink에서 전역 변수를 사용하려면 Data Store Memory 블록과 일치하는 변수 이름을 사용해야 합니다.

예를 들어, 다음 클래스 정의 파일은 각 시간 스텝마다 행렬의 첫 번째 행을 1씩 증분하는 System object를 정의합니다. 클래스 파일이 P 코딩되어 있는 경우 getGlobalNamesImpl을 포함시켜야 합니다.

classdef GlobalSysObjMatrix < matlab.System
   methods (Access = protected)
      function y = stepImpl(obj)
         global B;
         B(1,:) = B(1,:)+1;
         y = B;
      end

      % Include getGlobalNamesImpl only if the class file is P-coded.
      function globalNames = getGlobalNamesImpl(~)
         globalNames = {"B"};
      end
   end
end
다음 모델은 MATLAB System 블록과, 연결된 Data Store Memory 블록에 GlobalSysObjMatrix 객체가 포함되어 있습니다.

This image shows a model with a system object named GlobalSysObjMatrix that reads data from a datastore and writes a single output to a scope block and outport block

Main tab of the block parameters

Signal attributes tab of the block parameters