Can someone explain me this code?
조회 수: 6 (최근 30일)
이전 댓글 표시
error=(nargchk(2,5,nargin))%number of input arguments
if nargin < 5
h = zeros(length(A));
end
Here length of A can vary.
댓글 수: 0
답변 (1개)
dpb
2017년 3월 25일
편집: Steven Lord
2017년 3월 25일
What, specifically, do you not understand after reading
doc nargchk % which notes is deprecated, btw... and
doc zeros
What does the function statement definition look like and what do the comments (if any) say; both should give extensive clues.
I'll note the code as written is quite abnormal; unless there's a check later on nargchk here does absolutely nothing of any use; it stores an error message string in the variable error (thus aliasing the builtin error function, a very bad idea) but just continues on whether there is or is not an error generated.
The much more typical syntax (and a strong hint what you've posted may not be the actual code is the extra set of parentheses) would be
error(nargchk(2,5,nargin))
which would error if there were <2 or >5 inputs.
The allocation statement will also quite possibly not do what is intended; zeros with a single argument will generate a square array of that size, not a vector of the length given. Of course, A has to have been elsewhere defined or is one of the arguments or the function will crash there.
[SL: fixed two typos. "<1" -> "<2" and formatting of zeros in last paragraph.]
댓글 수: 2
Image Analyst
2017년 3월 25일
And if h is the output of the function, it should be declared/initialized before the error() function because usually after you throw an error you exit the function and if you haven't defined the output yet, then that will throw yet another error. So it would be like
function h = MyFunction(varargin)
h = []; % Initialize output
try
error(nargchk(2,5,nargin))
% Rest of code goes here....
catch ME
errorMessage = sprintf('Error in function MyFunction().\n\nError Message:\n%s', ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(errordlg(errorMessage));
return; % Make sure it bails out and runs no more code.
end
% Other, optional code.....
dpb
2017년 3월 25일
"_... after you throw an error you exit the function and if you haven't defined the output yet, then that will throw yet another error."_
Not if use the suggested idiom of wrapping nargchk inside error -- absolute minimal function:
>> type BM
function h=Bm(varargin)
error(nargchk(1,1,nargin))
>> h=Bm
Error using Bm (line 2)
Not enough input arguments.
>>
Fails as needs an argument, but h undefined doesn't error 'cuz it never gets there; error breaks back to command line immediately.
>>h=Bm(1)
Error in Bm (line 2)
error(nargchk(1,1,nargin))
Output argument "h" (and maybe others) not assigned during call to "C:\ML_R2014b\work\Bm.m>Bm".
>>
Does error of course now since there's an argument to satisfy that test but asked for a return value when called.
Not that it isn't "best practice" to have a null return value initialized, but it isn't mandatory if the error testing actually does evoke the error exit before there's a need for the return value. Just to be pedantic... :)
참고 항목
카테고리
Help Center 및 File Exchange에서 Encryption / Cryptography에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!