matlab中的nargchk,matlab   nargchk

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)显示出来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值