Force MATLAB to run a specific function.

7 visualizaciones (últimos 30 días)
Ed Davis
Ed Davis el 4 de Jun. de 2012
I understand MATLAB has an order of precedence for resolving names and that this can be useful in overriding the behavior of a MATLAB function. For example one can create a subfunction called sin that runs when called from within the main function instead of the builtin sin function. By the same token you can create a subfunction called fzero that runs instead of the supplied fzero function. In the case were I have a subfunction with the same name as a built-in function I can force MATLAB to run the builtin by using the 'builtin('function,x1,x1...) command. However, how to I do this with supplied functions such as fzero?
In short is there a way to force MATLAB to ignore the search path and run the function in a specific folder?

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Jun. de 2012
function varargout = fevalin(path, varargin)
oldfolder = cd(path);
[varargout{:}] = feval(varargin{:});
cd(oldfolder);
end
But with added sanity tests. And remember to cd back if you catch an error.
  2 comentarios
Ed Davis
Ed Davis el 4 de Jun. de 2012
Walter, Thanks for the help. So just to be clear I create a NEW function as you describe and pass the function name and arguments together with the directory were the function I actually want to run is located to this function. Then i can use this function to run a function located in a specific directory any time I want.
Thanks a lot.
Walter Roberson
Walter Roberson el 4 de Jun. de 2012
Yes, the syntax is an extension of feval()
I realized last night that this does not do exactly what you asked. When you use this or equivalent (e.g., manipulate the MATLAB path) then if the function calls a second function that you have loaded, then because the path has changed, the non-overloaded version would be called, whereas to be consistent the overloaded function should be called since the dispatching should affect *only* the named function and not anything else. I might be able to get around that if it is important.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables 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!

Translated by