Chaining function calls in Matlab

Matlab has a nice feature that lets you chain commands by treating functions as variables. This means that you can write:

a = "foo";
b = feval(a,arg1,arg2,...)

Now if we want to chain commands acting on the same input we can write:

  myfoos = cell{'foo1','foo2','foo3',...};
  x = ...
  chainedFoosOnX = applyChain(x,operators);
  function x = applyChain(x,operators)
    for ii = 1:length(operators) 
      x = feval(operators{ii},x);

Of course this example only works for one argument functions but it is simple to see how it would extend to more interesting scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *