More flexibility with anonymous function handles

8 views (last 30 days)
Hi there,
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:
%%
fs='myfun'
a=4; b=5; x=linspace(0,5,20)';
y=feval(fs,x,a,b)
%%
So, I could replace this with the "handle code"
fh=@(x)myfun(x,a,b);
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.
fh=fstr2func('myfun',x,a,b)
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.
fh=@(x)myfun(x,A(:,1),b)
won't work.
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.
Regards, MT
P.S. Sorry if this is addressed in another thread - I didn't find it.

Accepted Answer

Walter Roberson
Walter Roberson on 20 May 2012
fs=@myfun;
a=4; b=5; x=linspace(0,5,20)';
y=feval(fs,x,a,b)% or y=fs(x,a,b)
In newer MATLAB versions, you can str2func() a string that has @ syntax.
A function handle constructor does not need to give arguments and an expression (i.e., an anonymous function). And anonymous functions can specify varargin as part of their argument list. If you do need to bind in variables (e.g., extra arguments for a minimzer) you can use a function handle as part of an anonymous function.
fs=@myfun;
a=4; b=5; x=linspace(0,5,20)';
ode45(@(x) fs(x,a,b), x)

More Answers (1)

Mark T
Mark T on 21 May 2012
Dear Walter, many thanks for the reply. I didn't realise one could "double up" the "@" usage, i.e. to "dress up" an existing function handle. Similar to what you have above, I also tested:
fs=@myfun
a=4; b=5; x=linspace(0,5,20)';
fs2=@(x)fs(x,a,b)
y=fs2(x);
That should solve most of my problems.
Regards, MT

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!