Function code
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;
Note that the function code above uses a FEX code called function handle , which can be obtained from here .
Sample Use -
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
source share