MATLAB - Global Variables



Global variables in MATLAB are variables that can be accessed and modified from any part of your code, including within functions and scripts, without the need to pass them as function arguments or return values. While global variables can be convenient, they should be used with caution, as they can make your code less modular and harder to debug.

Important Things to Remember Regarding Global Variables

  • If the global variable is not present when you first use the global declaration, it will be initialized as an empty 0x0 matrix.
  • If a variable with an identical name as the global variable is already present in the current workspace, MATLAB will generate a warning and adjust both the value and scope of that variable to match the global variable.

Declaration of Global Variables

To declare a global variable in MATLAB, you need to use the global keyword followed by the variable name. This tells MATLAB that the variable should have global scope and can be accessed from anywhere in your code.

Syntax

global my_name;

Important things to remember about global variables −

  • When the global variable is used for the first time, and it doesn't exist, it is initialized as an empty 0x0 matrix.
  • If there is already a variable with a matching name as the global variable already present in the current workspace, MATLAB will issue a warning and synchronize the value and scope of the existing variable with the global variable.
  • When multiple functions declare a specific variable as global, they all refer to a common instance of that variable. Any modification made to this variable within one of the functions will be reflected in all other functions that have declared it as global, ensuring that all functions share the same data.

Advantages of Global Variables

  • Accessing Global variables − Global variables can be accessed from any part of your code, making them useful for sharing data between different functions, scripts, or workspaces.
  • Simplicity − They can simplify the passing of data between functions, especially in cases where multiple functions need access to the same data.

Disadvantages of Global Variables

  • Modifying Global Variables − Global variables can be modified from anywhere in your code, which can make it challenging to track changes and debug unexpected behavior.
  • Readability − Overuse of global variables can make your code less readable and harder to understand, as it's not clear where a variable's value is being modified.
  • Scope of Variables − If you use the same variable name as a global variable within a function or script, it can lead to scope confusion and unintended consequences.
  • Debugging Debugging becomes more difficult because you need to trace how and where a global variable's value is being modified.

Share Global Variable Between Functions

You can declare a variable as global and later use it in different functions by declaring it as a global variable inside the function body where you would like to access or modify it.

Here is the syntax

global myGlobalVar;

function myFunction()
   global myGlobalVar;
   % Access or modify myGlobalVar as needed
end

Let us see a working example. So now create a new script file and testglobalvariables.m and paste the below code in it.

global globalVar;
globalVar = 42;

disp(globalVar);
testGlobalVar()

function testGlobalVar()
   global globalVar;
   globalVar = globalVar + 10;
      disp(globalVar);
end

Now inside the matlab command window type test and press enter.

>> testglobalvariables
    42

    52

Clearing Global Variables from Workspace

Global variables have their own workspace which is separate from the base and functions workspace.

Once you are done using the global variable you can clear it from workspace by using hte command −

clear global variable

This will clear the global variable from all workspaces. Just to clear the global variable from current workspace simply use the command −

clear variable
Advertisements