功能代碼
function result = feval1(function_name,args)
%// Get the function filename by appending the extension - '.m'
relative_filename = strcat(function_name,'.m');
%// Get all possible paths to such a function with .m extension
pospaths = strcat(strsplit(path,';'),filesep,relative_filename);
%// All paths that have such function file(s)
existing_paths = pospaths(cellfun(@(x) exist(x,'file'),pospaths)>0);
%// Find logical indices for toolbox paths(if this function is a built-in one)
istoolbox_path = cellfun(@(x) strncmp(x,matlabroot,numel(matlabroot)),existing_paths);
%// Find the first toolbox and nontoolbox paths that have such a function file
first_toolbox_path = existing_paths(find(istoolbox_path,1,'first'));
first_nontoolbox_path = existing_paths(find(~istoolbox_path,1,'first'));
%// After deciding whether to use a toolbox function with the same function name
%// (if available) or the one in the current directory, create a function handle
%// based on the absolute path to the location of the function file
if ~isempty(first_toolbox_path)
func = function_handle(first_toolbox_path);
result = feval(func,args);
else
func = function_handle(first_nontoolbox_path);
result = feval(func,args);
end
return;
請注意上面的功能代碼使用名爲function handle
一個FEX的代碼,可以從here獲得。
使用範例 -
function_name = 'freqz'; %// sample function name
args = fircls1(54,0.3,0.02,0.008); %// sample input arguments to the sample function
result = feval1(function_name,args) %// output from function operation on input args
您可以創建在你的工作空間中的子文件夾包含「您」的功能。起初他們應該對你的主函數不知道,所以你可以檢查原函數的存在('which'等) - 如果結果爲空,你可以使用'addpath'來添加你的自定義函數的文件夾。使用面向對象的編程可能有更優雅的方法。但我並不熟悉這一點。 – thewaywewalk 2014-09-23 19:16:42
這裏提供的解決方案是否適合您? – Divakar 2014-09-29 09:14:39