MATLAB: Function masking / precedence of functions

functionmaskingMATLABprecedence

I have a question related functions and their precedence for the following example:
Assume I have a function (calling it function_main) which calls several other functions (function_1…function_n). The called functions are mainly very generic, e.g. Matlab intrinsic functions. I have started to translate the functions for my use cases into mex-functions calling them function_1_mex etc.. The translation does not cover the generic implementation, but only the use case in function_main. For those I can Unit Test them against their original Matlab implementation for exactly that usage. But the function_main is still under development and I want to test if new use cases appear compared to the current status of the translation. Thus I want to add an additional Unit Test which does the following:
Mask function_1 with function_1_mex and call the original function_main (that would otherwise not call any of my mex functions and see if it fails compared to the Matlab-only code, thus showing me that I now miss other use cases. In pseudo C-code I'd like something like this:
#define function_1(params[]) function_1_mex(params[]) // masking original function_1 for one call of function_main
function_main();
#undefined function_1
Is something comparable to that possible in Matlab? Maybe by changing the names of function_#_mex to function_# and changing the precedence for one call?

Best Answer

You could try the following: Run your code from a special directory that does not contain any of the functions you want to mask. Then for each function you want to mask, create an m-file of that name that actually calls the mex version of that function. This m-file in the current folder will mask any m-files that are in other directories on the path. This scheme will not work if the functions you want to mask are nested functions or local functions inside one of your m-files.
Related Question