Documentation Center

  • Trial Software
  • Product Updates

global

Declare global variables

Syntax

global X Y Z

Description

global X Y Z defines X, Y, and Z as global in scope.

Ordinarily, each MATLAB® function has its own local variables, which are separate from those of other functions, and from those of the base workspace. However, if several functions, and possibly the base workspace, all declare a particular name as global, they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the functions declaring it global.

If the global variable does not exist the first time you issue the global statement, it is initialized to the empty matrix.

If a variable with the same name as the global variable already exists in the current workspace, MATLAB issues a warning and changes the value of that variable to match the global.

Examples

Example 1

Type edit global_demo1 at the command line and enter the following function definition in a new file named global_demo1.m.

function global_demo1(num)
global globalvar

if nargin > 0
    globalvar = num;
end

fprintf('Global variable in function %s is %d\n', ...
    mfilename, globalvar);

This function declares a global variable named globalvar. If you pass a value when calling the function, the function stores the value in globalvar and then displays it. If you call the function with no arguments, the function just displays the value last written to the global workspace.

Create another function global_demo2 just like it. These two functions have separate function workspaces, but share a common global workspace:

copyfile global_demo1.m global_demo2.m

Call global_demo1, passing a numeric value. Then call global_demo2 with no value. You can see that the latter function has global access to the value that was passed to global_demo1.

global_demo1(1357);
Global variable in function global_demo1 is 1357

global_demo2
Global variable in function global_demo2 is 1357

Now set the value in global_demo2 and read it in global_demo1:

global_demo2(2468)
Global variable in function global_demo2 is 2468

global_demo1
Global variable in function global_demo1 is 2468

Example 2

Call the function global_demo1 that was defined in the previous example to assign a value to variable globalvar in the global workspace. Even though the variable is global, it is not accessible outside of the function workspace:

clear all
global_demo1(1357);
Global variable in function global_demo1 is 1357

if exist('globalvar', 'var')
    fprintf('Global variable is set to %d\n', globalvar);
else
    fprintf('Global variable is not available at the command line.\n');
end

Global variable is not available at the command line.

Now declare globalvar as a global variable at the MATLAB command line. Run the same statements to display the variable and this time you can see that the value assigned by the function is also available as a global variable in the base workspace:

global globalvar

if exist('globalvar', 'var')
    fprintf('Global variable is set to %d\n', globalvar);
else
    fprintf('Global variable is not available at the command line.\n');
end

Global variable is set to 1357

Example 3

Here is the code for the functions tic and toc (some comments abridged). These functions manipulate a stopwatch-like timer. The global variable TICTOC is shared by the two functions, but it is invisible in the base workspace or in any other functions that do not declare it.

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

More About

expand all

Tips

Use clear global variable to clear a global variable from the global workspace. Use clear variable to clear the global link from the current workspace without affecting the value of the global.

To use a global within a callback, declare the global, use it, then clear the global link from the workspace. This avoids declaring the global after it has been referenced. For example,

cbstr = sprintf('%s, %s, %s, %s, %s', ...
   'global MY_GLOBAL',                ...
   'MY_GLOBAL = 100',                 ...
   'disp(MY_GLOBAL)',                 ...
   'MY_GLOBAL = MY_GLOBAL+1',         ...
   'clear MY_GLOBAL');

uicontrol('style', 'pushbutton', 'CallBack', cbstr, ...
   'string', 'count')

There is no function form of the global command (i.e., you cannot use parentheses and quote the variable names).

See Also

| |

Was this topic helpful?