run returned function handle in one row

hi,
I have a funtion which returns a function handle acoording to an argument like:
function [hf]=foo(arg1)
...
hf=@foo1
function foo1(arg1)
...
How can I get the function handle for instance foo1 and run it in the same row?
I know that I can use feval, but I am looking for much more 'aesthetic' option. I use this for many functions what means dozens of rows which start with "feval".
Thanks, Eli

回答 (2 件)

Sean de Wolski
Sean de Wolski 2012 年 3 月 15 日

0 投票

I think the most aesthetic and best option is to split it into two rows.
Walter Roberson
Walter Roberson 2012 年 3 月 15 日

0 投票

DO = @(varargin) varargin{1}(varargin{2:end});
bar = {@cos,@sin};
foo = @(IDX) bar{IDX};
DO(foo(2),Pi/5)
Or perhaps you would prefer
for fun = foo(2); fun(Pi/5); end

この質問は閉じられています。

製品

質問済み:

Eli
2012 年 3 月 15 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by