속성을 초기화하고 일회성 계산을 설정하기
이 예제에서는 System object™를 초기화하고 설정하기 위한 코드를 작성하는 방법을 보여줍니다.
이 예제에서는 System object가 파일에 쓸 수 있도록 해당 파일을 열어 파일 리소스를 할당합니다. 이러한 초기화 작업은 객체를 실행할 때마다 수행하는 것이 아니라 설정 중에 일회적으로 수행합니다.
초기화할 퍼블릭 속성 정의하기
이 예제에서는 퍼블릭 Filename
속성을 정의하고 이 속성의 값을 조정 불가형 문자형 벡터 default.bin
으로 지정합니다. 사용자는 setup
메서드가 호출된 후에는 조정 불가형 속성을 변경할 수 없습니다.
properties (Nontunable) Filename = "default.bin" end
초기화할 프라이빗 속성 정의하기
사용자는 프라이빗 속성에 직접 액세스할 수 없으며 System object의 메서드를 통해서만 액세스할 수 있습니다. 이 예제에서는 pFileID
속성을 프라이빗 속성으로 정의합니다. 또한, 이 속성을 숨김으로 정의하여 사용자에게는 표시되지 않는 내부 속성임을 나타냅니다.
properties (Hidden,Access = private)
pFileID;
end
설정 정의하기
setupImpl
메서드를 사용하여 설정 및 초기화 작업을 수행합니다. 한 번만 실행하려는 코드는 setupImpl
메서드에 포함시켜야 합니다. setupImpl
메서드는 객체를 처음 실행할 때 한 번 호출됩니다. 이 예제에서는 이진 데이터를 작성하기 위해 파일을 열어 파일 리소스를 할당합니다.
methods function setupImpl(obj) obj.pFileID = fopen(obj.Filename,"wb"); if obj.pFileID < 0 error("Opening the file failed"); end end end
설정 과정에 포함되지는 않지만, 파일을 사용하여 코드를 완성하고 나면 파일을 닫아야 합니다. releaseImpl
메서드를 사용하여 리소스를 해제합니다.
초기화와 설정이 있는 완전한 클래스 정의 파일
classdef MyFile < matlab.System % MyFile write numbers to a file % These properties are nontunable. They cannot be changed % after the setup method has been called or the object % is running. properties (Nontunable) Filename = "default.bin" % the name of the file to create end % These properties are private. Customers can only access % these properties through methods on this object properties (Hidden,Access = private) pFileID; % The identifier of the file to open end methods (Access = protected) % In setup allocate any resources, which in this case % means opening the file. function setupImpl(obj) obj.pFileID = fopen(obj.Filename,'wb'); if obj.pFileID < 0 error("Opening the file failed"); end end % This System object writes the input to the file. function stepImpl(obj,data) fwrite(obj.pFileID,data); end % Use release to close the file to prevent the % file handle from being left open. function releaseImpl(obj) fclose(obj.pFileID); end end end
참고 항목
setupImpl
| releaseImpl
| stepImpl