Execute a list of functions

23 visualizaciones (últimos 30 días)
Hg
Hg el 2 de Nov. de 2016
Comentada: Hg el 2 de Nov. de 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) ...

Respuesta aceptada

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

Más respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by