MATLAB: How to use global variables and MATLAB workspaces

globalMATLABvariablesworkspaces

I would like to know more about how to use global variables and MATLAB workspaces.
Also, which workspaces are global variables stored in?

Best Answer

  • The following information should help you understand how to use global variables and MATLAB workspaces.
    MATLAB Workspaces
    *************************
    In order to understand global variables, you must first understand variables as they are normally stored in MATLAB.
    All variables in MATLAB are stored in a workspace. When manipulating data at the command line, the variables are stored in the MATLAB base workspace. The contents of this workspace can be displayed by using the whos command.
    MATLAB functions have their own workspaces, separate from the MATLAB workspace. Variables defined in this function workspace are automatically cleared from memory when the function returns.
    MATLAB script files store all variables in the calling workspace. If a MATLAB script is called from the command line, the variables are stored in the base workspace. If the same script is called from a function, the script stores its variables in the function's workspace, and the variables are cleared when the function returns.
    There is also a third type of workspace, the global workspace, where all global variables are stored. The contents of this workspace can be seen by using the whos global command. You cannot work directly in the global workspace, but variables in another workspace can be declared global using the GLOBAL command.
    Creating Global Variables
    ****************************
    Global variables are created using the global command. When the command
    global my_x
    is issued, the following process occurs:
    1. If a variable called my_x exists in the global workspace, then its value is assigned to the local workspace's variable my_x. Any existing local variable my_x has its value overwritten.
    2. If no variable called my_x exists in the global workspace, but there is a variable my_x in the current workspace, then the local variable my_x is placed in the global workspace.
    3. If no variable exists in either the current workspace or the global workspace, then a new variable my_x is created and given the value [] (the empty matrix). This new variable is placed in the global workspace.
    4. The local variable and the global variable are then linked.
    Note that there is opportunity for confusion here. Suppose you want a function MyFun to share a global variable, my_x, with the main MATLAB workspace, where it has a value. If you declare my_x global in MyFun first, then the global value for my_x becomes [], the empty matrix. If you then declare my_x global in the main MATLAB workspace, this empty matrix overwrites the local value for my_x, and its initial value is lost. Declaring the variable global in the main MATLAB workspace first, and then in MyFun, does the right thing -- the initial value for my_x is retained. Care must be taken to avoid this kind of mistake.
    Tips for Working with the Global Workspace
    ***************************************************
    Use care when naming global variables. Since all functions use the same global workspace, naming conflicts can easily occur. To avoid this, use unique, descriptive variable names for all global variables. Since variable names are case sensitive, capitalization can also be used to keep global variable names unique. For example, instead of using x, use Selected_X_Coord.
    The commands CLEAR, WHO, and WHOS normally apply to the current workspace. These commands take an optional argument, global, which specifies that the command should be applied to the global workspace. For example, to show the contents of the global workspace, type at the MATLAB prompt:
    whos global
    Clearing a variable from the local workspace (using 'clear my_x') breaks the link between the local and global variables, but does not erase the global variable.
    Clearing a variable from the global workspace (using 'clear global my_x') completely erases the variable my_x from the local and global workspaces.
    The ISGLOBAL function can be used to determine if a link exists between a local variable and the global workspace. This function does not determine if a variable exists in the global workspace; if the variable exists in the global workspace, but no link exists from a local variable, ISGLOBAL returns a zero (false).
    The command PACK clears all variables from the global workspace. After PACK is issued, the global workspace is empty, and no variables have links to the global workspace. Therefore, take care when using the PACK command to re-declare variables as global after making the call to PACK.
    Relevant commands: clear, global, isglobal, who, whos, pack