数学建模社区-数学中国
标题:
Matlab关于nargin和nargout函数程序不能正常运行
[打印本页]
作者:
hfut-liu
时间:
2009-9-8 17:27
标题:
Matlab关于nargin和nargout函数程序不能正常运行
function [y1,y2]=mytest(x1,x2)
- [7 _$ P' g9 V5 w" ^ d, @
if nargin==1
7 i5 m7 t& I- S7 Q6 e m, m# N
y1=x1;
' V' N, d, N, A9 [0 w5 D
if nargout==2
& ] d% W0 _& j
y2=x1;
$ Y; M/ ]$ B) W' O: \5 c( n
end
. B. y3 l1 A9 L+ M! C
else
. m$ G4 O+ L9 @ C" C
if nargout==1
: A- r; D' G4 G3 t
y1=x1+x2;
, [8 m4 }3 N2 H# C! r# a) H9 a! {
else
. K/ _) v$ V/ _* b9 P( j
y1=x1;
) F0 T% u7 c1 s( p
y2=x2;
3 h( K2 e' ~% _% {) P$ n; ~
end
+ f2 a0 D3 m2 r! X" Y
end
% w5 b/ G J+ R7 t, p" O
这个程序是关于函数调用时实际的输入和输出参数个数,结合条件分支语句就可以处理函数调用中指定不同数目的输入输出参数的情况。
+ Z) [4 ]: N$ m% l) O8 K
这个程序应该没有问题但是报错
3 S3 v. ]7 B3 j0 X2 C: w1 T8 K
function [y1,y2]=mytest(x1,x2)
3 X% A$ ?! v3 f/ @& c# G) O, B
|
; k- z0 J4 r! A. D- a+ t
Error: Function definitions are not permitted at the prompt or in scripts.
: e3 V5 v3 q3 i4 Q6 x
请大家帮忙看看,谢谢
作者:
madio
时间:
2009-9-9 02:12
函数要定义在M文件中,不要在命令窗口输入!
作者:
粟归玉
时间:
2009-9-10 00:46
jiayou!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5