nargin表示输入参数.
输入参数个数: 1<=nargin<=2.
二,matlab 中有两条代码:
msg=nargchk(2,2,nargin);
error(msg);
看不懂哦,请高手指点,谢谢!
当一个函数的输入参量的个数超出了规定的范围,MATLAB函数nargchk提供了统一的响应。函数nargchk给定为:
function msg = nargchk(low, high, number)
% NARGCHK Check number of input arguments.
% Return error message if not between low and high.
% If it is, return empty matrix.
% Copyright (c) 1984-94 by The MathWorks, Inc.
msg = [ ] ;
if (number < low)
msg = ' Not enough input arguments. ' ;
elseif (number > high)
msg = ' Too many input arguments. ' ;
end
下列的文件片段表明了在一个M文件函数内的典型用法:
error(nargchk(nargin, 2, 5))
如上所示,如果nargin的值小于2,函数error象前面描述的那样进行处理,nargchk返回字符串'没有足够的输入参量。'。如果nargin的值大于5,函数error执行处理,nargchk返回字符串'太多输入参量。'。如果nargin是在2和5之间,函数error简单地将控制传递给下一个语句,nargchk返
回一个空字符串。也就是说,当它的输入参量为空,error函数什么也不做。
英文解释如下:
MATLAB Function Reference
nargchk
Check number of input arguments
Syntax
msg = nargchk(low,high,number)
Description
The nargchk function often is used inside an M-file to check that the correct number of arguments have been passed.
msg = nargchk(low,high,number) returns an error message if number is less than low or greater than high. If number is between low and high (inclusive), nargchk returns an empty matrix.
Arguments
Input arguments to nargchk are
low, high
The minimum and maximum number of input arguments that should be passed.
number
The number of arguments actually passed, as determined by the nargin function.
Examples
Given the function foo:
function f = foo(x,y,z)
error(nargchk(2,3,nargin))
Then typing foo(1) produces:
Not enough input arguments.
这是用来在M文件中检查你的输入的变量数是否合格,你可以设置第一个参数low,即最小的传递参数的数目,第二个参数high,最大的传递参数个数。这里两个都是2,则必须是传递两个参数。如果参数个数满足大于low,小于high,则返回一个空矩阵,否则返回错误消息,由error(msg)显示出来。