How to Check if a Variable Exists in Workspace MATLAB

Mehak Mubarik Feb 02, 2024
  1. Check the Variable Under Observation in the Workspace Without Using Any Built-In Function in MATLAB
  2. Check the Variable Under Observation in the Workspace Using Exist Function in MATLAB
How to Check if a Variable Exists in Workspace MATLAB

We will look at different ways to check the existence of variables under observation in the workspace in MATLAB.

The variable can be anything from a local variable to a function. We can check the existence of our variable using the exist function and without function.

The exist function gives numeric numbers ranging from 0 to 9. Each numeric number has its meaning depending on the variable we search for.

Let us start with checking the existence of variables under observation in the workspace without using any built-in function in MATLAB.

Check the Variable Under Observation in the Workspace Without Using Any Built-In Function in MATLAB

For this purpose, we will design a function according to our variable requirements. Let our variables a, b, c, and d equal 1.

We define our function as check_workspace_variables(). The argument to be given to the function will be the name of the variable we want to see.

a = 1;
b = 1;
c = 1;
d = 1;

check_workspace_variables('d')
check_workspace_variables('b')
check_workspace_variables('c')
check_workspace_variables('e')

function our_output = check_workspace_variables(variable_check)
% Check to see if a variable exists in the Base Workspace

does_string_exists = sprintf('exist(''%s'')',variable_check);
our_workspace_variables = evalin('base',does_string_exists);

if our_workspace_variables == 1 % If variable exists in our workspace in MATLAB
disp('Is Present in our Workspace')
our_output = 1 ;
else % If variable doesnot exist in our workspace in MATLAB
disp('Is Absent from our Workspace')
our_output = 0 ;
end
end

Output:

check_variable_presence
Is Present in our Workspace

ans = 1

Is Present in our Workspace

ans = 1

Is Present in our Workspace

ans = 1

Is Absent from our Workspace

ans = 0

In this example, we checked for the variables a, b, c, and e if you carefully look at the variables defined in the code.

Any variable named e does not exist. That is why three answers came back as 1 and the last one as 0.

Check the Variable Under Observation in the Workspace Using Exist Function in MATLAB

Let us understand this concept by creating a random matrix using the magic() function and naming it as our_variable. We will use the exist function to check if our_variable exists in our workspace in MATLAB or not.

our_variable = magic(5)
exist our_variable

Output:

our_variable =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
 
ans = 1

As per the pre-defined numeric outputs of the function in MATLAB, 1 means that the name of our variable exists in the workspace in MATLAB.

Mehak Mubarik avatar Mehak Mubarik avatar

Mehak is an electrical engineer, a technical content writer, a team collaborator and a digital marketing enthusiast. She loves sketching and playing table tennis. Nature is what attracts her the most.

LinkedIn

Related Article - MATLAB Variable