How to call a function in a particular toolbox (or overload a function name and call the original)
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brian Rasnow
el 5 de Oct. de 2023
Respondida: Steven Lord
el 6 de Oct. de 2023
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 comentario
Respuesta aceptada
Matt J
el 6 de Oct. de 2023
Editada: Matt J
el 6 de Oct. de 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 comentario
Matt J
el 6 de Oct. de 2023
Editada: Matt J
el 6 de Oct. de 2023
Here's an example that avoids repeated disk access.
sin(0)
sin(pi/2)
sin()
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
Más respuestas (2)
Steven Lord
el 6 de Oct. de 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 comentarios
Walter Roberson
el 6 de Oct. de 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 comentarios
Walter Roberson
el 6 de Oct. de 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
el 6 de Oct. de 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.
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!