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);
    end
  end

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

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>