函数概念帮助:检查工作区MATLAB中是否存在变量

时间:2020-03-05 10:25:44

标签: matlab function variables workspace concept

我坚持使用这个非常简单的功能。我犯了一些根本看不到的基本概念性错误。任何帮助将不胜感激。

我想使用代码检查工作空间中是否存在某个变量。如果不是,则不应该执行任何操作,否则应执行某些操作以在文件中进行读取。 谢谢

最小的可复制示例:

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

示例: a = 1; b = 1; c = 1:d = 1:

测试功能:

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

函数的输出:

Variable NOT Present

ans =

     0

Variable Present

ans =

     1

Variable Present

ans =

     1

Variable Present

ans =

     1

2 个答案:

答案 0 :(得分:2)

代码有两个问题:

1)当函数调用时,它返回函数工作区中而不是基础工作区中可用的变量列表。如果您从第一行代码中删除分号,则会看到该函数的输出:

workspace_variables = who('-regexp',variable_to_check)

从命令行运行该函数时,您会看到该函数在执行此行时只有一个变量,并且该变量是输入变量“ variable_to_check”:

>> workspace_variables_check('b')

workspace_variables =

  1×1 cell array

  {'variable_to_check'}

所有变量a,b,c等都在“基本”工作区中,并且单独的函数无法访问它们。 函数可以使用哪些变量的概念称为 scope 。这是指向blog post的链接,该链接讨论了MATLAB的范围。

2)发生的另一件事是,同一行代码对存在的变量名(字符串'variable_to_check')执行正则表达式。因此,字符“ a”,“ b”,“ c”都由正则表达式匹配,但“ d”不匹配。因此,您可以检查一个神秘变量“ v”:

>> workspace_variables_check('v')

workspace_variables =

  1×1 cell array

  {'variable_to_check'}

Present from Workspace

还包括“ ch”,“ var”等。我敢打赌,这会使调试令人困惑:)

如果要让函数检查“基本”工作空间(在命令行中使用的变量)中的变量,则可以使用以下方法:

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

答案 1 :(得分:0)

您正在寻找功能exist。实际上,您想执行以下操作

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

请注意,无论您是否指定搜索类型(此处为'var'),该函数都会独立返回整数代码,但出于速度和清晰度的考虑,建议使用该函数。

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.)