How to execute a function which has the same name as another variable?

I defined two functions as follows:
function output=l2()
D=1;
C=2;
clear C
output=C*D;
end
function output=C()
output=0;
end
The point here is test whether a function can be called when there is another variable has the same name as the function does. Apparently, without line 'clear C', the output of l2 will be 2 due to the function precedence order. After removing C from workspace of l2, however, there is an error message coming out while executing line 'output=C*D':
>> l2
Reference to a cleared variable C.
Error in l2 (line 5)
output=C*D;
It seems to me that matlab somehow recognizes C as a variable when it sees line 'C=2'.It will remember the type of C as an variable even if the latter is removed from workspace, so it will ignore that there is another function also named C and report an error when evaluating the following line, even if the line is totally legit. Am I understanding it correctly?
Nonetheless, even stranger, if I execute function l2 in debug mode with break point on line 'clear C', and type 'output=C*D' in command line after running 'clear C', it would work, no error reported. If at this time I press F10 to let debug process proceed, the same error as above would be shown. So it is so strange to me that matlab sometimes knows where to find alternative meaning behind a name (function instead of variable in the case above) but sometimes it just cannot.
Could anyone please explain the reason why matlab fails to execute 'C' as a function in this case? Note that C is already deleted from workspace. And more importantly, is it possible to add some code between line 'clear C' and 'output=C*D' to implement what I intend to do here? I do want to keep other parts of code unchanged.

Answers (1)

The bottom line here is probably "don't do that" in your code. This is similar to using eval( ) or load( ) to pop variables into your code that the parser knows nothing about when it reads your code into memory. The rules for exactly how the parser works are not published, so you as the programmer shouldn't rely on any critical assumptions about how you think it should work. Changing the definition of a token in the middle of your code is (obvious from your example) apparently something the parser is not equiped to handle. The parser saw that C was a variable when the code was first read into memory and was not set up to redefining this later on in your code when you cleared C and suddenly the function C was not shadowed anymore.
Running code in debug mode has additional differences, as you have discovered. This has been documented in other posts in past years. Apparently different parsed code runs in the debugger than what gets run outside of the debugger. In most cases results will be the same, but there have been several documented cases on this forum where this has not been the case. So yes, unfortunately there are differences that can arise. You just need to make sure your code is robust enough to handle these types of differences.
I think the solution in your case is to write code so that the variable C and the function C are never in the same scope anywhere in your code. That way the parser will never be confused as to what you really mean. E.g., if you want to retain both variable C and function C by those names, then in the scope where variable C exists call function E which in turn calls function C.

Categories

Asked:

on 27 Jul 2017

Edited:

on 27 Jul 2017

Community Treasure Hunt

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

Start Hunting!