How to call a function in a particular toolbox (or overload a function name and call the original)

7 次查看(过去 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
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
...
  1 个评论
Matt J
Matt J 2023-10-6
编辑:Matt J 2023-10-6
Here's an example that avoids repeated disk access.
sin(0)
ans = 0
sin(pi/2)
ans = 1
sin()
SIN called with no input - using local version
function out=sin(t)
persistent hsin
if nargin
if isempty(hsin), hsin=load('sinHandle').h; end
out=hsin(t);
else
disp 'SIN called with no input - using local version'
end
end

请先登录,再进行评论。

更多回答(2 个)

Steven Lord
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.

Walter Roberson
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
Walter Roberson 2023-10-6
testfuncout()
outer call no flag outer call with flag inner call
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 CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by