加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab里面主函数调用子函数改变主函数里面变量

(2015-09-29 16:53:43)
标签:

matlab函数调用

matlab里面主函数声明变量未个global类型,同时在子函数里面也要声明变量为global类型
如下:
子函数为
function month=myfunction1(month)
global month1;
len1=length(month);
for i=1:1:len1
     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
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)

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有