How to select a specific .m function when two exist? -
first, here way i'm calling function :
eval([functionname '(''stringarg'')']); % functionname = 'somestringforthefunctionname' now, have 2 functionname functions in path, 1 take stringarg , 1 takes else. i'm getting errors because right first 1 finds function doesn't take stringarg. considering way i'm calling functionname function, how possible call correct function?
edit:
i tried function which :
which -all somestringforthefunctionname the result :
c:\........\x\somestringforthefunctionname c:\........\y\somestringforthefunctionname % shadowed the shadowed function 1 want call.
function names must unique in matlab. if not, there duplicate names, matlab uses first 1 finds on search path.
having said that, there few options open you.
option 1. use @ directories, putting each version in separate directory. using ability of matlab apply function specific classes. so, might set pair of directories:
@char @double put copies of myfun.m in respective directories. when matlab sees double input myfun, direct call double version. when matlab gets char input, goes char version.
be careful. not put these @ directories explicitly on search path. put them inside directory on search path.
a problem scheme if call function single precision input, matlab have fit, need separate versions single, uint8, int8, int32, etc. cannot have 1 version numeric types.
option 2. have 1 version of function, tests first argument see if numeric or char, branches perform either task appropriate. both pieces of code in 1 file then. simple scheme have subfunctions or nested functions work.
option 3. name functions differently. hey, not end of world.
option 4: shaun points out, 1 can change current directory. matlab looks first in current directory, find function in directory needed. 1 problem time consuming. time touch directory, things slow down, because there disk input needed.
the worst part of changing directories in how use matlab. (imho) poor programming style force user in specific directory based on code inputs wish run. better data driven scheme. if reading in or writing out data, in directory. use matlab search path categorize of functions, functions tend not change much. far cleaner way work requiring user migrate specific directories based on how calling given function.
personally, i'd tend suggest option 2 best. clean. has 1 main function need work with. if want keep functions district, put them separate nested or sub functions inside main function body. inside of course, have distinct names, based on how driven.
Comments
Post a Comment