it seems one must come to accept function handles as unavoidable, given that so many function-functions slowly don't support function name strings anymore. There are clearly some nice new forms of syntax that are quite practical, but also apparently some drawbacks.
(1) Most of the time, I want to define the function names at the top of the code (as a string), and almost always these functions will take multiple parameters. As an example:
a=4; b=5; x=linspace(0,5,20)';
So, I could replace this with the "handle code"
y=fh(x) % or y=feval(fh,x)
But here the function name is hard-coded, i.e. the line 'fh=' must occur after creation of a and b. str2func.m does not seem to work with anonymous functions. How can one "dynamically" construct this function handle? (besides using a very ugly eval command).
I tried writing a few helper functions using evalin.m (caller) and inputnames.m, i.e.
produced the same as invoking the command: fh=@(x)myfun(x,a,b)
but this only works when using "pure" variables (due to inputname.m), e.g.
Are there some lower level function handle functions that would give more control?
(2) It seems a bit scary that with the code, A=rand(1000); fh=@(x)myfun(x,A)
somehow stores the values of A with the function handle. If A is reassigned, the function handle still uses the old values, so they are still in the workspace somewhere. But a whos on fh only shows 32 bytes... interesting.
So if I reassign the values in A, I need always to reassign fh - all the more reason to want some smooth way of constructing the anonymous function handle dynamically.
What I can imagine is that anonymous function handles are more efficient (allowing better acceleration, etc.) - can someone point me to a Mathworks doc where this is stated? Because I don't really see them as being easier to use.
P.S. Sorry if this is addressed in another thread - I didn't find it.