Tarun
Tarun

Reputation: 3

Function Conceptual Help: Check if a variable exists in workspace MATLAB

I am stuck with this very simple function. I am making some fundamental conceptual mistake which I cannot see. Any help would be super appreciated.

I want to use the code to check if a certain variable is present in the work space. If it is then no operation should be performed else a certain operation should be performed for ex read in a file. Thanks

Minimal reproducible example:

function workspace_variables_check(variable_to_check)
% Loop over all the Variables in the Workspace to get the all the variable names
workspace_variables = who('-regexp',variable_to_check);
if isempty(workspace_variables) % A variable by this name is present in the workspace
    disp('Absent in Workspace')
    output = 1 ;
else                            % A variable by this name is not present in the workspace
    disp('Present from Workspace')
    output = 0 ;
end

Example : a = 1; b = 1; c = 1: d = 1:

Test the function:

workspace_variables_check('d')
workspace_variables_check('b')
workspace_variables_check('c')
workspace_variables_check('a')

Output of the function:

Variable NOT Present

ans =

     0

Variable Present

ans =

     1

Variable Present

ans =

     1

Variable Present

ans =

     1

Upvotes: 0

Views: 1903

Answers (2)

mhopeng
mhopeng

Reputation: 1081

There are two problems with the code:

1) When who is called by the function, it returns the list of variables available in the function workspace, not in the base workspace. If you remove the semicolon from the first line of code, you will see the output from the function:

workspace_variables = who('-regexp',variable_to_check)

When you run the function from the command line, you see that the function has only one variable when this line executes, and that variable is the input variable "variable_to_check":

>> workspace_variables_check('b')

workspace_variables =

  1×1 cell array

  {'variable_to_check'}

All of the variables a,b,c etc are in the "base" workspace, and a separate function does not have access to them. The concept of which variables are available to a function is called scope. Here is a link to a blog post that discusses scope in MATLAB.

2) The other thing that is happening is that the same line of code performs a regexp against the name of the variables present, which is the string 'variable_to_check'. So the characters 'a','b','c' are all matched by the regexp, but 'd' does not. So you can check for a mystery variable "v":

>> workspace_variables_check('v')

workspace_variables =

  1×1 cell array

  {'variable_to_check'}

Present from Workspace

Also "ch", "var", etc. I bet that made the debugging confusing :)

If you want a function to check for variables in the "base" workspace (which is what you use from the command line), you can use this:

function output = workspace_variables_check(variable_to_check)
% Check to see if a variable exists in the Base Workspace

exist_string = sprintf('exist(''%s'')',variable_to_check);
workspace_variables = evalin('base',exist_string);

if workspace_variables == 1      % A variable by this name is present in the workspace
    disp('Present from Workspace')
    output = 1 ;
else                        % A variable by this name is not present in the workspace
    disp('Absent in Workspace')
    output = 0 ;
end

Upvotes: 2

Max
Max

Reputation: 4045

You are looking for the function exist. In fact you want to do the following

if exist(variable_to_check,'var') == 1
% do something
end

Note that the function will return the integer code independently whether you specify the search type (here 'var') or not but it is recommended for speed and clarity.

0 — name does not exist or cannot be found for other reasons. For example, if name exists in a restricted folder to which MATLAB® does not have access, exist returns 0.
1 — name is a variable in the workspace.
2 — name is a file with extension .m, .mlx, or .mlapp, or name is the name of a file with a non-registered file extension (.mat, .fig, .txt).
3 — name is a MEX-file on your MATLAB search path.
4 — name is a loaded Simulink® model or a Simulink model or library file on your MATLAB search path.
5 — name is a built-in MATLAB function. This does not include classes.
6 — name is a P-code file on your MATLAB search path.
7 — name is a folder.
8 — name is a class. (exist returns 0 for Java classes if you start MATLAB with the -nojvm option.)

Upvotes: 0

Related Questions