sun1993 发表于 2007-8-28 12:01

看不懂有关 nargin 的程序代码

John H. Mathews著的数值方法(matlab版)中第308页有个求极小值的单纯形法程序,
一开始的 if nargin==5
               show=0;
            end
    %show==1displays iterations(P and Q)
没弄明白,这是用来做什么呀?
各位大侠,能否教我一下?谢谢!

[ 本帖最后由 eight 于 2007-8-28 18:56 编辑 ]

jimin 发表于 2007-8-28 14:00

nargin, nargout
Purpose
Number of function arguments.

Synopsis
n = nargin
n = nargout

Description
In the body of a function M-file, nargin and nargout indicate how many input or output arguments, respectively, a user has supplied.
nargin returns the number of input arguments specified for a function.

nargout returns the number of output arguments specified for a function.

Examples
This example shows portions of the code for the function fplot, which has an optional number of input and output arguments:

function = fplot(fname,lims,npts,angl,subdiv)
%FPLOT Plot a function.
% FPLOT(fname,lims,npts,angl,subdiv)

% The first two input arguments are
% required; the other three have default values.
...
% = fplot(...) returns x and y instead
% of plotting them.
...
if nargin < 5, subdiv = 20; end
if nargin < 4, angl = 10; end
if nargin < 3, npts = 25; end
...
if nargout == 0
plot(x,y)
else
x0 = x;
y0 = y;
end

jimin 发表于 2007-8-28 14:03

nargin用法示例。
    函数文件examp.m:
function fout=charray(a,b,c)
if nargin==1
   fout=a;
elseif nargin==2
   fout=a+b;
elseif nargin==3
   fout=(a*b*c)/2;
end
    命令文件mydemo.m:
x=;
y=;
examp(x)
examp(x,y')
examp(x,y,3)

sun1993 发表于 2007-8-28 17:59

好厉害,谢谢!:@D

xjzuo 发表于 2007-8-29 16:22

简单讲,就是判断输入变量个数.------help nargin 即知.
页: [1]
查看完整版本: 看不懂有关 nargin 的程序代码