Filter löschen
Filter löschen

Execute a list of functions

13 Ansichten (letzte 30 Tage)
Hg
Hg am 2 Nov. 2016
Kommentiert: Hg am 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) ...

Akzeptierte Antwort

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

Weitere Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by