How to call a function in a particular toolbox (or overload a function name and call the original)
7 views (last 30 days)
Show older comments
I use filtfilt.m a lot but wish to give it some default arguments. I can do the following:
y = function myfiltfilt(x)
if nargin == 1, y = filtfilt([.25 .5 .25],1,x); end % call the signal toolbox with default args
...
But is there a better way, where I could overload a filtfilt.m in a local directory, which calls the default function of the same name in the signal toolbox?
Thanks!!
1 Comment
Accepted Answer
Matt J
on 6 Oct 2023
Edited: Matt J
on 6 Oct 2023
You could save a handle to the true filtfilt function in a .mat file and then do something like this,
function y = filtfilt(x)
h=load('filfiltHandle.mat').h;
if nargin == 1, y = h([.25 .5 .25],1,x); end % call the signal toolbox with default args
...
1 Comment
More Answers (2)
Steven Lord
on 6 Oct 2023
You could do this by defining a class with a method named filtfilt, calling that method with an instance of the class (either as a dummy input or by converting one of the inputs into an instance of that class), and calling the function filtfilt from Signal Processing Toolbox with raw numeric data (including some extracted from the class instance?) rather than the instance of the class.
But I agree with @Stephen23 that a simpler approach would be to create and call a wrapper function with a different name.
0 Comments
Walter Roberson
on 6 Oct 2023
Your personal filtfilt.m file would have to cd to a different directory, and then run a function that removed the directory with your filtfilt from the path, and then call a second function that then called filtfilt . Because you removed your filtfilt from the path and it would not be in the current directory, then the filtfilt invoked from the second function is not going to resolve to your filtfilt and so would be able to reach the toolbox filtfilt. Make sure that you have an onCleanup to cd back to the currect directory.
Inside your filtfilt.m there is no way for your filtfilt.m to say, "resolve filtfit ignoring the current directory" (for the case that your filtfilt is being found because it is in the current directory instead of because it is on the MATLAB path), and there is no way to say "resolve filtfilt ignoring a particular directory" (for the case that your filtfilt is on the MATLAB path). The only operation for anything similar to that is builtin which deals strictly with builtin functions.
3 Comments
Walter Roberson
on 6 Oct 2023
"The feval function follows the same scoping and precedence rules as calling a function handle directly. For more information, see Create Function Handle."
Walter Roberson
on 6 Oct 2023
testfuncout()
function testfuncout(arg)
if nargin == 0
disp('outer call no flag')
testfuncin()
else
disp('outer call with flag')
end
function testfuncin
feval('testfuncout', 1)
testfuncout(2)
function testfuncout(arg)
disp('inner call');
end
end
end
This tells us that feval() passed a named function does not know about nested functions -- it has to go through name resolution as if the function is being called from outside the current file.
However... if the current directory has not changed and the MATLAB path has not changed, then if you feval('filtfilt') from inside your filtfilt.m then the "public" function filtfilt is still going to resolve to your filtfilt.m . So using feval() might be useful for resolving a function outside of the current file, it does nothing to solve the problem you are dealing with, where you want the current function file to be ignored.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!