MATLAB: How to get the function handle to the currently running function without relying upon the current path settings

function handleMATLABshadowed

The question says it all, but here it is again. How can I get the function handle to the currently running function without relying upon the current path settings?
function myFunction();
fxn = @myFunction;
doesn't work because myFunction is shadowed by a myFunction function which isn't the current function, so fxn is a handle to the wrong myFunction.
function myFunction();
fxn = str2func(mfilename());
fails for the same reason.
function myFunction();
fxn = str2func(mfilename('fullpath'));
is not a valid way to call str2func.
Any ideas?
P.S. – For the curious, I'm trying to run a unit test on a recursive function. To create mockup functions, I have a special path full of mockup functions which is added to the path after I get the function handle to the function which I'm testing. This allows functions within the function being tested to be replaced with mockups that simplify testing. However, this behavior with a recursive function actually prevents the test from being very useful.

Best Answer

One approach that you can take is to exploit the fact that the local functions have higher precedence than the functions that may be found on the path. Thus you should just be able to introduce one layer of function indirection in order to always dispatch to your recursive algorithm. So, instead of thi:
function out = myFunction(in)
stuff = doStuff;
out = myFunction(stuff);
end
It would look like this:
function out = myFunction(in)
out = myLocalFunction(in);
end
function out = myLocalFunction(in)
stuff = doStuff;
out = myLocalFunction(stuff);
end
This seems like it would work for you and be much cleaner and more efficient than path or current folder manipulation. However, I am confused because it seems like you want to dispatch always to the production myFunction so why is there a test double on the path at all? I guess I don't see why it is a problem because you don't seem to ever want to dispatch to the test double at all. Am I missing something?