suner1986 发表于 2009-8-4 09:47:05

MATLAB中的全局变量

请问如何让MATLAB中的全局变量实现类似C中的全局变量的功能呢?用GLOBAL就可以了吗

晓夜寒风 发表于 2009-8-5 13:20:08

是的,可以参考PSAT的文件,首先声明,
再次使用时用global,类似于C

suner1986 发表于 2009-8-7 11:22:45

请问版主,具体是参考PSAT的什么文件呢

晓夜寒风 发表于 2009-8-7 23:50:24

本帖最后由 晓夜寒风 于 2009-8-7 23:56 编辑

If the global variable does not exist the first time you issue the global statement, it is initialized to the empty matrix.
即 假设定义x 为全局变量
   则 使用 global x
编译的时候 解释器第一次遇到 ”global x   ” 就为其分配空间,
以后再从其他文件里遇到 x,就使其值与之前的x 相同。


举例

注意 文件一 与文件二 共享了 TICTOC 这个变量,在文件三中实现了时间长度计算!

文件一

function ticjh
%    TIC Start a stopwatch timer.
%      TIC; any stuff; TOC
%    prints the time required.
%    See also: TOC, CLOCK.
global TICTOC
TICTOC = clock;
文件二
function t = tocjh
%    TOC Read the stopwatch timer.
%    TOC prints the elapsed time since TIC was used.
%    t = TOC; saves elapsed time in t, does not print.
%    See also: TIC, ETIME.
global TICTOC
if nargout < 1
    elapsed_time = etime(clock, TICTOC)
else
    t = etime(clock, TICTOC);
end
文件三
ticjh;
for i=1:1:100
    for j=1:1:100
      
    end
end
t=tocjh;
PSAT 中应用了很多global。

燕尾鸢 发表于 2015-4-6 13:10:24

学习了!谢谢
页: [1]
查看完整版本: MATLAB中的全局变量

招聘斑竹