How can I get the function handle to the currently running function without relying upon the current path settings?
Mostrar comentarios más antiguos
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.
1 comentario
Daniel Shub
el 18 de Abr. de 2013
Could you put the mockup functions in a package instead of adding the paths?
Respuesta aceptada
Más respuestas (2)
Roshin Kadanna Pally
el 17 de Abr. de 2013
0 votos
Something to try:
You can determine this apriori and pass it as one of the input arguments to myFunction or save it in a mat file and load it back in your function.
fxn = @myFunction;
Execute above for the function you would be running beforehand. This need to be done only once and you can avoid shadowing by Cd-ing to the correct location.
function myFunction(fxn)
% fxn will always point to the currently running function
% Or, load a mat file that has fxn saved
3 comentarios
Michael
el 18 de Abr. de 2013
Walter Roberson
el 18 de Abr. de 2013
So you don't need the workspace of the current instantiation of the function?
Michael
el 23 de Abr. de 2013
Image Analyst
el 7 de Oct. de 2014
0 votos
Why can't you simply call dbstack() to get the currently running function?
2 comentarios
Michael
el 7 de Oct. de 2014
Image Analyst
el 7 de Oct. de 2014
Editada: Image Analyst
el 7 de Oct. de 2014
mfilename is only the name of the m-file. dbstack gives the complete list of all the functions that have been called. So if main.m called fun1, which called fun2 which called fun3, dbstack would give you all of that (main->fun1->fun2->fun3) while mfilename could only give main.m. Though I don't think it's in the form of a "function handle."
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!