声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1401|回复: 4

[编程技巧] 看不懂有关 nargin 的程序代码

[复制链接]
发表于 2007-8-28 12:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

[ 本帖最后由 eight 于 2007-8-28 18:56 编辑 ]
回复
分享到:

使用道具 举报

发表于 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 [x0,y0] = 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.
...
% [x,y] = 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
发表于 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=[1:3];
y=[1;2;3];
examp(x)
examp(x,y')
examp(x,y,3)

评分

1

查看全部评分

 楼主| 发表于 2007-8-28 17:59 | 显示全部楼层
好厉害,谢谢!:@D
发表于 2007-8-29 16:22 | 显示全部楼层
简单讲,就是判断输入变量个数.------help nargin 即知.
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-10-2 22:23 , Processed in 0.067751 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表