Phonon
Phonon

Reputation: 12737

Checking if my function overrides another function

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

Consider the following hypothetical scenario. I'm implementing a functional called freqz which may exist in MATLAB if the Signal Processing Toolbox is installed. If it does indeed already exist as a part of the toolbox, I want to call it inside my own and return its result. If it does not exist, I would like my own function to do its own processing.

Here's a sample pseudocode

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'll get the version they expect, and fall back on my own implementation if foo is unavailable from elsewhere. Is MATLAB capable of doing something like this?

What I've tried:

Upvotes: 5

Views: 101

Answers (3)

Divakar
Divakar

Reputation: 221664

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;

Please note the above function code uses a FEX code named function handle that can be obtained from here.

Sample usage -

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

Upvotes: 0

Stewie Griffin
Stewie Griffin

Reputation: 14939

By calling which you can get the full path of any function. Assuming you don't put any custom functions inside folders named toolbox, this seems to work quite well:

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

Now, to check if any of these are in any of your installed toolboxes:

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

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

You can also check for 'built-in' in the findstr, but I find that some functions from toolboxes doesn't have this in front of the name.

Upvotes: 2

Pieter21
Pieter21

Reputation: 1845

Just two suggestions, not a real answer.

Maybe by finding the script by name (which foo) http://www.mathworks.nl/help/matlab/ref/which.html but that probably also points to the foo you're already in.

Otherwise, you'd have to search the complete path for occurrences of foo.

Upvotes: 0

Related Questions