2011-12-25 65 views
3

在MATLAB说你做:Matlab的 - 我怎么知道一个变量名是否可以自由使用

E = cell(3,1); 

如何知道自己是否被已被使用E和上面的电话不会覆盖它?我必须运行程序并在那个时间休息吗?解释器中有没有一种方法可以为我做到这一点?例如,在C++中,编译器会告诉你是否尝试使用现有名称。

+0

Duplicate:http://stackoverflow.com/questions/674474/how-to-determine-whether-a-matrix-is-empty-or-not-in-matlab/674477#674477 – 2011-12-25 13:30:02

+1

@Audrey。这不是一个重复的问题。 OP不询问变量值是否为空。问题是关于确定变量是否已被使用。 – Kavka 2011-12-25 21:41:47

+0

@kavka,好吧,你是对的。 – 2011-12-26 14:10:15

回答

0

为了使用存在,您必须首先运行该脚本,以便工作空间将填充您正在使用的所有变量。如果您想在编写脚本时检查变量名是否可用,我最喜欢的方法是使用Matlab IDE中的Tab键。它会提出所有的自动完成选项。如果您之前在脚本或函数中定义了变量名称“E”,则键入“E”应显示E作为选项,并提醒您不要使用该变量。

此外,IDE的最新版本引入了自动突出显示脚本中给定变量的所有用法。只需将光标置于字母之间或变量名称的末尾即可。在脚本中直观地检查变量名称的所有用法非常方便。

8

this page,你应该使用命令exist

help exist 
EXIST Check if variables or functions are defined. 
    EXIST('A') returns: 
    0 if A does not exist 
    1 if A is a variable in the workspace 
    2 if A is an M-file on MATLAB's search path. It also returns 2 when 
     A is the full pathname to a file or when A is the name of an 
     ordinary file on MATLAB's search path 
    3 if A is a MEX- or DLL-file on MATLAB's search path 
    4 if A is a MDL-file on MATLAB's search path 
    5 if A is a built-in MATLAB function 
    6 if A is a P-file on MATLAB's search path 
    7 if A is a directory 
    8 if A is a Java class 
+0

在这种情况下,应该使用“1”。 – 2011-12-25 13:23:20

+3

这是'exist('E','var')'检查是否存在一个变量'E',它顺便比'exists'('E')== 1'更易于理解。 – Egon 2011-12-25 13:40:24

1

用途:

if isempty (whos('E')) 
    % variable can be used 
end 
0

您可以使用checkcodemlint做的MATLAB文件静态分析,其中除其他事项外如果在函数中使用变量之前应该报告变量。

相关问题