Is it possible that we override global variables?

Posted by Ram Moj on Stack Overflow See other posts from Stack Overflow or by Ram Moj
Published on 2012-11-06T05:27:03Z Indexed on 2012/11/06 11:01 UTC
Read the original article Hit count: 251

Filed under:

I have this function:

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

and I expect TICTOC=5 change the result of toc, since TICTOC is a global variable in tic and toc functions; but this is not the case; Does anyone know the reason?

I like to know the answer, because I 'm worried to declare a global variable, which it's name has been declared global in some other functions, I'm not aware of.

I saw this function in matlab 2008b help

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

thanks.

© Stack Overflow or respective owner

Related posts about matlab