Execute a list of functions

14 次查看(过去 30 天)
Hg
Hg 2016-11-2
评论: Hg 2016-11-2
How do I store a list of user-defined function in an array and execute it in a loop one by one?
[d] = func1(a),
[e] = func2(b),
[f] = func2(c) ...

采纳的回答

Walter Roberson
Walter Roberson 2016-11-2
myfun = {@func1, @func2, @func2} ;
vars = {a, b, c};
for K = 1 : length(myfun)
result{K} = myfun{K}(vars{K});
end

更多回答(1 个)

KSSV
KSSV 2016-11-2
编辑:KSSV 2016-11-2
myfun = {'func1','func2','func3'} ; % write function names in a cell
d = feval(myfun{1},a) ; % calls first function
e = feval(myfun{2},b) ; % calls second function
f = feval(myfun{3},c) ; % calls third function
doc feval.
  1 个评论
Hg
Hg 2016-11-2
This also works but Walter's answer is closer (regarding looping)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by