Execute a list of functions

27 views (last 30 days)
Hg
Hg on 2 Nov 2016
Commented: Hg on 2 Nov 2016
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) ...

Accepted Answer

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

More Answers (1)

KSSV
KSSV on 2 Nov 2016
Edited: KSSV on 2 Nov 2016
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 Comment
Hg
Hg on 2 Nov 2016
This also works but Walter's answer is closer (regarding looping)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!