Reputation: 12184
In MatLab say you do:
E = cell(3,1);
How do I know whether E is being used already and the call above doesn't override it? Do I have to run the program and break at that point? Is there a method in the interpreter which will do this for me? For instance, in C++, the compiler will tell you if you try to use an existing name.
Upvotes: 3
Views: 1909
Reputation: 12917
According to this page, you should use the command 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
Upvotes: 8
Reputation: 3244
In order to use exist, you must run the script first so the workspace will be populated with all the variables you are using. If you want to check and see if a variable name is free when you're writing the script, my favorite way to check is to use the tab key in the Matlab IDE. It will bring up all the auto-complete options. If you've previously defined a variable names "E" in your script or function, typing "E" should show E as an option and alert you not to use that variable.
Also, the latest version of the IDE introduced automatic highlighting of all uses of a given variable in a script. Just put the cursor in between the letters or at the end of the variable name. It's quite handy to visually inspect all uses of a variable name in your script.
Upvotes: 0