Force MATLAB to run a specific function.

12 ビュー (過去 30 日間)
Ed Davis
Ed Davis 2012 年 6 月 4 日
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?

採用された回答

Walter Roberson
Walter Roberson 2012 年 6 月 4 日
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 件のコメント
Ed Davis
Ed Davis 2012 年 6 月 4 日
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 2012 年 6 月 4 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by