views:

1859

answers:

4

I'd like to do something like this:

>> foo = @() functionCall1() functionCall2()

so that when I said

>> foo()

it would execute functionCall1() and then execute functionCall2(). (I feel that I need something like the C , operator)

EDIT:

functionCall1 and functionCall2 are not necessarily functions that return values.

+1  A: 

Maybe I am missing somethign, just make a function combinationCall that calls both functions for you.

MatlabDoug
well, I'm hoping to be able to do it all from the command window... would that be possible?
Daniel LeCheminant
You probably could, but why bother? Use a script and a function file. It makes it easier to iterate through your work by just running the driver script.
MatlabDoug
+7  A: 

Trying to do everything via the command line without saving functions in m-files may be a complicated and messy endeavor, but here's one way I came up with...

First, make your anonymous functions and put them in a cell array:

fcn1 = @() ...;
fcn2 = @() ...;
fcn3 = @() ...;
fcnArray = {fcn1 fcn2 fcn3};

...or, if you have functions already defined (like in m-files), add the handles to the cell array like so:

fcnArray = {@fcn1 @fcn2 @fcn3};

Then you can make a new anonymous function that calls each function in the array:

foo = @() cellfun(@feval,fcnArray);

Although funny-looking, it works.

EDIT: If the functions in fcnArray need to be called with input arguments, you would first have to make sure that ALL of the functions in the array require THE SAME number of inputs. In that case, the following example shows how to call the array of functions with one input argument each:

foo = @(x) cellfun(@feval,fcnArray,x);
inArgs = {1 'a' [1 2 3]};
foo(inArgs);  % Passes 1 to fcn1, 'a' to fcn2, and [1 2 3] to fcn3
gnovice
sweet! i've been trying to figure out something similar.
Jason S
+2  A: 

The anonymous function syntax in Matlab (like some other languages) only allows a single expression. Furthermore, it has different variable binding semantics (variables which are not in the argument list have their values lexically bound at function creation time, instead of references being bound). This simplicity allows Mathworks to do some optimizations behind the scenes and avoid a lot of messy scoping and object lifetime issues when using them in scripts.

If you are defining this anonymous function within a function (not a script), you can create named inner functions. Inner functions have normal lexical reference binding and allow arbitrary numbers of statements.

function F = createfcn(a,...)
  F = @myfunc;
  function b = myfunc(...)
    a = a+1; 
    b = a; 
  end
end

Sometimes you can get away with tricks like gnovice's suggestion.

Be careful about using eval... it's very inefficient (it bypasses the JIT), and Matlab's optimizer can get confused between variables and functions from the outer scope that are used inside the eval expression. It's also hard to debug and/or extent code that uses eval.

Mr Fooz
A: 

If functionCall1() and functionCall2() return something and those somethings can be concatenated, then you can do this:

>> foo = @() [functionCall1(), functionCall2()]

or

>> foo = @() [functionCall1(); functionCall2()]

A side effect of this is that foo() will return the concatenation of whatever functionCall1() and functionCall2() return.

I don't know if the execution order of functionCall1() and functionCall2() is guaranteed.

ManWithSleeve
Yeah, unfortunately I'm dealing with functions that don't return values.
Daniel LeCheminant