Verifying that my function overrides another function

I am trying to find out at runtime whether my function overrides another function.

Consider the following hypothetical scenario. I implement a functional called freqz that can exist in MATLAB if the signal processing tools are installed. If it really already exists as part of the toolkit, I want to call it inside myself and return its result. If it does not exist, I would like my own function to do its own processing.

Here is an example of pseudo code

 function foo(args) if overrides_another_function(foo) func = find_overriden_function(foo); result = func(args); else result = my_own_processing(args); return result; 

In this case, when someone calls foo , they will get the expected version and return to my own implementation if foo not available from other sources. Can MATLAB do something like this?

What I tried:

  • The call to exist inside foo always returns 2 (the function exists), because the function is considered declared as soon as we are inside it for the first time.
  • Executing exist from outside a function in an m file is invalid MATLAB syntax.
  • I did not find a way to list all the functions with the given name. If possible, it will help me halfway (I would even know about the existence, but still I would have to figure out how to access the overridden function).
+5
source share
3 answers

By calling which , you can get the full path to any function. Assuming you don't put any custom functions inside folders called toolbox , this looks very good:

 x = which('abs', '-all'); %// Returns a cell array with all the full path %// functions called abs in order of precedence 

Now, to check if there is any of them in any of the toolboxes you have installed:

 in_toolbox = any(cellfun(@(c) any(findstr('toolbox',c)), x)); 

This will return true if the 'abs' function already exists in one of your toolboxes, and 0 if it is not. From there, I think it should be possible to avoid using your own custom.

You can also check the 'built-in' in findstr , but I found that some functions from the toolbars do not have this before the name.

+2
source

Only two sentences, not a real answer.

Perhaps finding a script by name (which is foo) http://www.mathworks.nl/help/matlab/ref/which.html but this probably also indicates the foo you're already in.

Otherwise, you will have to look for the full path for the occurrences of foo.

0
source

Function code

 function result = feval1(function_name,args) %// Get the function filename by appending the extension - '.m' relative_filename = strcat(function_name,'.m'); %// Get all possible paths to such a function with .m extension pospaths = strcat(strsplit(path,';'),filesep,relative_filename); %// All paths that have such function file(s) existing_paths = pospaths(cellfun(@(x) exist(x,'file'),pospaths)>0); %// Find logical indices for toolbox paths(if this function is a built-in one) istoolbox_path = cellfun(@(x) strncmp(x,matlabroot,numel(matlabroot)),existing_paths); %// Find the first toolbox and nontoolbox paths that have such a function file first_toolbox_path = existing_paths(find(istoolbox_path,1,'first')); first_nontoolbox_path = existing_paths(find(~istoolbox_path,1,'first')); %// After deciding whether to use a toolbox function with the same function name %// (if available) or the one in the current directory, create a function handle %// based on the absolute path to the location of the function file if ~isempty(first_toolbox_path) func = function_handle(first_toolbox_path); result = feval(func,args); else func = function_handle(first_nontoolbox_path); result = feval(func,args); end return; 

Note that the function code above uses a FEX code called function handle , which can be obtained from here .

Sample Use -

 function_name = 'freqz'; %// sample function name args = fircls1(54,0.3,0.02,0.008); %// sample input arguments to the sample function result = feval1(function_name,args) %// output from function operation on input args 
0
source

Source: https://habr.com/ru/post/1203247/


All Articles