matlab里面主函数调用子函数改变主函数里面变量
(2015-09-29 16:53:43)
标签:
matlab函数调用 |
matlab里面主函数声明变量未个global类型,同时在子函数里面也要声明变量为global类型
for
j=1:1:8
if(month(i,j)>=10)
str=num2str(month(i,j));
str1=str(1);
integer=str2num(str1);
str2=str(2);
if(str2=='0')
month(i,j)=integer;
else if(str2=='3')
month(i,j)=integer+0.333;
else
month(i,j)=integer+0.667;
end
end
else
;
end
end
如下:
子函数为
function month=myfunction1(month)
global month1;
len1=length(month);
for i=1:1:len1
end
end
主函数为:
function ppp()
global month1;
month1=zeros(31,8);
moth1=myfunction1(month1);
end
子函数里面不要用全局变量作为输入:
你将变量a定义为全局变量,但是在调用子函数时候采用subfunction(a,b),实际上这时候是的a是你传入的变量值,不一定是全局变量的a,我试验了一下:
function yp = my
%LOTKA Lotka-Volterra predator-prey model.
global ALPHA;
ALPHA=2;
yp=3*timess(9);
end
function z=timess(ALPHA)
%global ALPHA;
z=2*ALPHA;
end
结果不是全局变量的a=2而是传入的9,故而结果是:54(想象应该12)
function yp = my
%LOTKA Lotka-Volterra predator-prey model.
global ALPHA;
ALPHA=2;
yp=3*timess(9);
end
function z=timess(ALPHA)
%global ALPHA;
z=2*ALPHA;
end
结果不是全局变量的a=2而是传入的9,故而结果是:54(想象应该12)