Force MATLAB to run a specific function.

7 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jun 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 Comments
Ed Davis
Ed Davis on 4 Jun 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 on 4 Jun 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!