If you ever found yourself in a situation where you could not or didn't want to add a directory to the MATLAB search path, but still needed speedy access to a function in that directory, this file is for you.
In such cases, having to change directories to gain access to the function is not the best solution: you'd always have to take care to change the current path back to what it was (even on error). Moreover, performance in those cases can be poor if you have to call this function very often.
FUNCTION_HANDLE allows you to create function handles which can successfully be evaluated without loss of performance, even if the function the handle points to is not on the MATLAB search path.
While there often are better ways to accomplish this sort of task (package directories, symbolic links, etc.), there are a few niche cases where these solutions are simply more involved than using this FUNCTION_HANDLE constructor.
Note that FUNCTION_HANDLE overloads a function present in standard MATLAB. This 'native' function is nothing more than documentation (linked to in the help for this constructor) and an error message which says that you cannot use the function to construct handles. As this is exactly what FUNCTION_HANDLE implements, this shadowing is desirable.
>> F = function_handle('./path/to/function/myFcn.m')
>> A = function_handle(...
Rody Oldenhuis (2020). Constructor for function_handles (https://www.github.com/rodyo/FEX-function_handle), GitHub. Retrieved .
Would you please elaborate a bit more on how I can use this with my function in matlab. I am not very good in matlab but know this is very useful for my work.
Worked exactly as I needed. Thank you so much!
Very nice. I needed to call some private functions, including a mex file, from a folder other than the private's parent. This worked perfectly.
I like the idea and it is nicely implemented. Thank you Roy!
[linked to Github]
- small doc update; tags were messed up