0

我有这个功能:

function example(y)
global TICTOC;
tic
TICTOC=5;
toc
end

我希望TICTOC=5改变toc的结果,因为TICTOC是 tic 和 toc 函数中的全局变量;但这种情况并非如此; 有谁知道原因?

我想知道答案,因为我担心声明一个全局变量,它的名称已在其他一些函数中声明为全局变量,我不知道。

我在 matlab 2008b 帮助中看到了这个函数

function tic
%    TIC Start a stopwatch timer.
%        TIC; any stuff; TOC
%    prints the time required.
%    See also: TOC, CLOCK.
global TICTOC
TICTOC = clock;

function t = toc
%    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

谢谢。

4

2 回答 2

0

我认为您可以使用 assignin 命令将 TICTOC 值发送到基础,从而更改全局值。我使用 assignin 命令将参数从函数发送到 Base。

问候迪利普

于 2012-11-06T10:58:57.283 回答
0

我不知道为什么,但我的问题的答案是否定的。我检查了它,似乎它没有被覆盖。原因一定是因为tic、toc是Matlab内置的函数

于 2012-11-28T17:15:09.907 回答