returns a function handle from a target (string, function library, or function itself) fh = parse_fh(target) if given a string, uses str2func to retrieve function handle if given a function handle, returns that function handle function library: a function that, given an argument, will return the function handle for a sub-function of that function. if given a cell array of 2 strings, parse_fh will attempt to turn the first string into a function handle (str2func) and then will pass the second string as an argument to the first string/function, and will expect a function handle to return as the first product of this function this is the convention that is being used in stimulus selection function libraries (see private/matlab/projects/stim_markup/stim_markup_library.m for an example) FB 2009.04.20
0001 function fh = parse_fh(target) 0002 0003 % returns a function handle from a target (string, function library, or function itself) 0004 % 0005 % fh = parse_fh(target) 0006 % 0007 % if given a string, uses str2func to retrieve function handle 0008 % if given a function handle, returns that function handle 0009 % 0010 % function library: a function that, given an argument, will return the 0011 % function handle for a sub-function of that function. 0012 % 0013 % if given a cell array of 2 strings, parse_fh will attempt to turn the 0014 % first string into a function handle (str2func) and then will pass the 0015 % second string as an argument to the first string/function, and will 0016 % expect a function handle to return as the first product of this function 0017 % this is the convention that is being used in stimulus selection function 0018 % libraries (see private/matlab/projects/stim_markup/stim_markup_library.m 0019 % for an example) 0020 % 0021 % FB 2009.04.20 0022 0023 if ischar(target) 0024 % target is a string, use str2func 0025 fh = str2func(target); 0026 elseif iscell(target) 0027 % target is a cell ... could this be intended for a function library? 0028 if length(target) ~= 2 || ... 0029 ~ischar(target{1}) || ... 0030 ~ischar(target{2}) 0031 error(['must provide library name and sub-function for '... 0032 'function library reference\n']); 0033 end 0034 lfh = str2func(target{1}); 0035 fh = lfh(target{2}); 0036 elseif isa(target,'function_handle') 0037 fh = target; 0038 else 0039 error(['must provide either a function string, cell array of strings '... 0040 'for a function library call, or a valid function\n']); 0041 end