More flexibility with anonymous function handles

조회 수: 7 (최근 30일)
Mark T
Mark T 2012년 5월 20일
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.

채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 20일
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)

추가 답변 (1개)

Mark T
Mark T 2012년 5월 21일
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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by