Main Content

속성값과 입력값의 유효성 검사하기

이 예제에서는 System object™에 지정된 입력값 및 속성값이 유효한지 확인하는 방법을 보여줍니다.

단일 속성의 유효성 검사하기

다른 속성에 독립적인 하나의 속성값에 대해 유효성을 검사하려면 MATLAB 클래스 속성 유효성 검사를 사용하십시오. 이 예제에서는 논리 속성, 양의 정수 속성 및 문자열 속성(세 값 중 하나여야 함)을 지정하는 방법을 보여줍니다.

 properties
    UseIncrement (1,1) logical = false
    WrapValue (1,1) {mustBePositive, mustBeInteger} = 1
    Color (1,1) string {mustBeMember(Color, ["red","green","blue"])} = "red"
 end

상호 종속적 속성의 유효성 검사하기

두 개 이상의 상호 종속적 속성에 대해 유효성을 검사하려면 validatePropertiesImpl을 사용하십시오. 이 예제에서는 논리 속성(UseIncrement)이 true 이고 WrapValue 값이 Increment보다 큰지 확인하는 validatePropertiesImpl을 작성하는 방법을 보여줍니다.

 methods (Access = protected)
    function validatePropertiesImpl(obj)
        if obj.UseIncrement && obj.WrapValue > obj.Increment
          error("Wrap value must be less than increment value");
        end
    end
 end

입력값의 유효성 검사하기

입력값의 유효성을 검사하려면 validateInputsImpl 메서드를 사용하십시오. 이 예제에서는 첫 번째 입력값이 숫자형 값인지 검사하는 방법을 보여줍니다.

methods (Access = protected)
   function validateInputsImpl(~,x)
      if ~isnumeric(x)
         error("Input must be numeric");
      end
   end
end        

전체 클래스 예제

이 예제는 각 유효성 검사 구문 유형의 예를 보여주는 전체 System object입니다.

classdef AddOne < matlab.System
% ADDONE Compute an output value by incrementing the input value
  
  % All properties occur inside a properties declaration.
  % These properties have public access (the default)
  properties
    UseIncrement (1,1) logical = false
    WrapValue (1,1) {mustBePositive, mustBeInteger} = 10
    Increment (1,1) {mustBePositive, mustBeInteger} = 1
  end

  methods (Access = protected)
    function validatePropertiesImpl(obj)
        if obj.UseIncrement && obj.WrapValue > obj.Increment
          error("Wrap value must be less than increment value");
        end
    end
    
    % Validate the inputs to the object
    function validateInputsImpl(~,x)
        if ~isnumeric(x)
          error("Input must be numeric");
        end
    end
        
    function out = stepImpl(obj,in)
      if obj.UseIncrement
        out = in + obj.Increment;
      else
        out = in + 1;
      end
    end    
  end
end

참고 항목

|

관련 항목