Checking the range of input arguments
이전 댓글 표시
I have a function where two of the input parameters (a and b) must be within the range
. So I have written the following code to check if the input arguments are valid:
if ~(varargin{1} > 0 && varargin{1} < 1), error('Parameters must be in the interval [0,1]')
else
a = varargin{1};
end
if ~(varargin{2} > 0 && varargin{2} < 1), error('Parameters must be in the interval [0,1]')
else
b = varargin{2};
end
But I am repeating the same process twice, so I am wondering if there is a more compact way to apply the same criterion to both parameters simultaneously. Is there a shorter way to write this code?
Any suggestions would be greatly appreciated.
채택된 답변
추가 답변 (1개)
Sajeer Modavan
2019년 4월 14일
if ~(varargin{1,2} > 0 && varargin{1,2} < 1), error('Parameters must be in the interval [0,1]')
else
a = varargin{1};
b = varargin{2};
end
댓글 수: 1
Guillaume
2019년 4월 14일
varargin is always a row vector. So varargin{1, 2} is always equivalent to varargin{2}.
The above only checks that the 2nd argument is valid.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!