How to call a function in a particular toolbox (or overload a function name and call the original)
11 次查看(过去 30 天)
显示 更早的评论
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!!
采纳的回答
Matt J
2023-10-6
编辑:Matt J
2023-10-6
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
...
更多回答(2 个)
Steven Lord
2023-10-6
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 个评论
Walter Roberson
2023-10-6
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 个评论
Walter Roberson
2023-10-6
"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
2023-10-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!