How to check if any variables share a name with a built-in function?

12 views (last 30 days)
I'm compiling our code as a standalone executable using 'mcc' or 'deploytool'. Compilation fails if any variable names are also function names. For example, using a variable called 'image' results in failure ('image.m' is a function); changing the variable name to 'image2' solves the problem (there's no function called 'image2.m').
Since our code isn't simple, checking variable names across all our scripts and functions could be time consuming. Is there a simple way to do this?
I can imagine a solution where we run our code, get the list of all variable names in memory, and try calling help on that variable names. However, variables are cleared at multiple parts in the code, so this isn't straightforward.
We're using Matlab 2016a.
  1 Comment
dpb
dpb on 21 Apr 2017
Something similar to that is what TMW suggests in the Advanced Programming documentation hints; afaik there is no real toolset to do such; a real hassle/shortcoming I think for serious development in Matlab. That aliasing is silent always is a real killer imo. Add to it the name pollution with the proliferation of new features plus toolboxen and it's amazing there's any meaningful names left, almost!
I've toyed with the idea of building a command line tool which would grep files for lists of functions created from the subdirectory tree but the hassle in general is need to be able to parse the user code at least to the extent of being able to identify all functions. Those in their own m- or p-files are easy, of course; just use their directory listing but need to pull out those that may not be found that way...
Probably that could get reasonably close start, but what it misses are the hard ones, anyway, though. Altho maybe there would be few enough left to just then let the compiler complain, maybe???
I'd probably start somewhere along that line....

Sign in to comment.

Accepted Answer

Sebastian Castro
Sebastian Castro on 21 Apr 2017
Edited: Sebastian Castro on 21 Apr 2017
You can use the which function. For example:
>> image = 2;
>> which -all image
image is a variable.
built-in (C:\Program Files\MATLAB\R2017a\toolbox\matlab\specgraph\image) % Shadowed
If you want to use this as part of your code, you can call the same function in a way that returns a cell array of text. You can then parse through this as needed.
>> x = which('-all','image');
Sebastian

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!