Matlab LogoHow do global variables differ from regular (local) variables?

Each function in MATLAB contains a set of variables specific to that function.  Even in the same .m file, you don’t have (direct) access to variables created in other functions within the file.  Global variables give you the ability to create/change a variable in one function and have that updated variable accessible elsewhere.  This post will discuss two methods for handling (no pun intended) global variables, one of which is perfectly integrated into Graphical User Interfaces (GUIs).

METHOD 1: global VARIABLE

The first (non-GUI) way to create a global variable is to use the function ‘global’.  Create the global variables X, Y, and Z with the command:

global  X Y Z

The ‘global’ function needs to be called in each separate function (usually in the beginning) where the variables will be called.   Stylistically, the variable names are usually longer names and all in CAPS to indicate global variables within the functions.  The documented example in the MATLAB helps shows this pretty well:

function tic
global TICTOC %define/incorporate global variable at start of function
TICTOC = clock;

function t = toc
global TICTOC %accesses variable TICTOC (or creates it if TICTOC is undefined)
if nargout < 1
    elapsed_time = etime(clock, TICTOC)
else
    t = etime(clock, TICTOC);
end

Many hard-core coders prefer to avoid using ‘global’ except for constants.  The reason behind this is because it’s generally considered poor form to lock up a variable name (See Steve L’s comment below for another reason!).  While this won’t matter for smaller programs and functions, when the files get to be many hundreds (or thousands or millions) of lines long, it can be very difficult to keep track of all of the global variables and to remember to call all the necessary variables at the start of each function.  The great thing about GUIs is that they already have a built-in global structure to deal with all of your global variables: the handles.  The handles structure is an input (and therefore accessible) to every function in the GUI, making it perfectly capable doing everything the ‘global’ command can.  In fact, you shouldn’t ever have to use ‘global’ command when designing a GUI because the handles structure does the job so well.  GUIs and ‘global’ don’t mix kids!

METHOD 2: handles.variable

As you may have seen from many of the blinkdagger GUI tutorials, the handles structure is an extremely useful method to manipulate GUI boxes/buttons/tools.  But the tool data are all just stored variables that can be accessed anywhere within the GUI (aka global variables!).  Since we don’t need to edit any ‘property’ of the handles structure (e.g. handles.static_text, ‘String’), we don’t need to use the ‘get’/’set’ commands.  Creating the global variable is as easy as saying:

handles.x = 42;
%And of course, don't forget to update your handles structure:
guidata(hObject, handles);

handles.x is now an independent variable and note that it has no relation to the local variable x.

x = 43;

is a completely valid command in the same function that would not overwrite your global variable ‘handles.x’.

Remember, these variables can range from constants (e.g. 12) to strings (e.g. ‘Hello World’) to structures, cells, and arrays of constants/strings.

Hopefully you can see the usefullness of global variables and will use them (properly!) in your coding adventures.