功能代码
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