필터 지우기
필터 지우기

narginchk and varargin in MATLAB

조회 수: 3 (최근 30일)
DM
DM 2015년 4월 20일
댓글: Walter Roberson 2023년 4월 20일
I have come across PART of a code in MATLAB in a toolbox that I don't understand
elseif (txMode==2)
narginchk(7, 7);
numTx = varargin{1};
numRx = varargin{2};
switch numTx
case 2
numCSRRE_RB = 4*2*2;
case 4
numCSRRE_RB = 4*3*2;
end
I don't understand what `narginchk` and `varargin` are used for in this example, and why is that the output of `varargin{1}` would be either 2 or 4 (a conclusion I came up with when looking at the code after `switch`).
Thanks

답변 (2개)

James Tursa
James Tursa 2015년 4월 20일
narginchk(7, 7) throws an error if the number of input arguments is less than 7 or greater than 7 (i.e., the number of input arguments must be exactly 7 in this case or an error will be thrown).
varargin{1} is the first input argument.
varargin{2} is the second input argument.
Why the value of varargin{1} should be 2 or 4 would depend on the code in question and how it is called.

Lei
Lei 2023년 4월 20일
편집: Walter Roberson 2023년 4월 20일
Recode
function p= marie (p,q)
narginchk[2,3]
disp('Yo, G!')
a=p+q
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 4월 20일
%if the user passes in no parameters then none of the parameters will exist
%if the user passes in too few parameters then variable p will not exist
%if the user passes in more than 3 parameters then there will be a 4th
%parameter and that should be rejected
%
%note that we are accepting a third parameter even though we do not do
%anything with it. This is because the original code permitted either 2 or
%3 inputs.
function p = marie (p,q,extra_ok,extra_not_ok)
if ~exist(p, 'var') || exist('extra_not_ok', 'var')
error('You must pass in either 2 or 3 inputs')
end
disp('Yo, G!')
a=p+q
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by