How can I distinguish between a variable and a MATLAB file that have the same name in MATLAB 7.6 (R2008a)?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jan 2010
Edited: MathWorks Support Team
on 17 Feb 2021
I have the following line of code
g = example(4)
I would like to be able to determine if this is referencing the variable example or calling the function MATLAB function example.
Accepted Answer
MathWorks Support Team
on 17 Feb 2021
Edited: MathWorks Support Team
on 17 Feb 2021
There are two functions that MATLAB provides that might be useful. The best option will depend on the specific application.
1) You can use the WHICH command, which returns the function or variable that will be called by a particular line of code. For example, the following command will return what will be executed when the command example(3) is issued. The output will indicate if it is a variable or a function.
which('example(3)');
You may request outputs from WHICH and parse them to do this programmatically:
x = which('example(3)');
You can read more about the WHICH command at the following website:
2) You can use the EXIST command. The following example assumes that there is a file on the MATLAB path called "example2.m".
tst_name='example2';
if exist(tst_name, 'var')==1
fprintf('%s is a variable.\n', tst_name);
end
if exist(tst_name, 'file')==2
fprintf('%s is a file on the MATLAB path.\n', tst_name);
end
You can read more about the EXIST command at the following website:
0 Comments
More Answers (0)
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!