global - Declare variables as global - MATLAB (original) (raw)
Main Content
Declare variables as global
Syntax
Description
Note
Global variables are inefficient and make errors difficult to diagnose. Use a function with input variables instead. For more information see Avoid Unnecessary Copies of Data.
global var1 ... varN
declares the specified variables 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 all declare a particular variable name as global
, then they all share a single copy of that variable. Any change of value to that variable, in any function, is visible to all the functions that declare it as global.
If the global variable does not exist the first time you issue the global
statement, it is initialized to an empty 0x0
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 and its scope to match the global variable.
Examples
Create a function in your current working folder that sets the value of a global variable.
function setGlobalx(val) global x x = val;
Create a function in your current working folder that returns the value of a global variable. These two functions have separate function workspaces, but they both can access the global variable.
function r = getGlobalx global x r = x;
Set the value of the global variable, x
, and obtain it from a different workspace.
setGlobalx(1138) r = getGlobalx
Assign a value to the global variable using the function that you defined in the previous example.
Display the value of the global variable, x
. Even though the variable is global, it is not accessible at the command line.
Undefined function or variable 'x'.
Declare x
as a global variable at the command line, and display its value.
Change the value of x
and use the function that you defined in the previous example to return the global value from a different workspace.
Tips
- To clear a global variable from all workspaces, use
clear global
variable
. - To clear a global variable from the current workspace but not other workspaces, use
clear
variable
. - Global variables have their own workspace, which is separate from the base and function workspaces. Using global variables is inefficient. MATLAB applies memory optimization to input variables in functions, but not to global variables. For more information see Avoid Unnecessary Copies of Data.
Also, global variables carry notable risks. Any function can access and update a global variable. Other functions that use the variable might return unexpected results. For example:- If you unintentionally give a “new” global variable the same name as an existing global variable, one function can overwrite the values expected by another. This error is difficult to diagnose.
- If a global variable is changed in multiple functions, calling those functions in a different order can lead different results.
Use global variables sparingly, if at all. For more information on variables and the workspace, see Global Variables subsection on Share Data Between Workspaces page.
Version History
Introduced before R2006a