Wanted: Matlab example of an anonymous function returning more than 1 output

I use anonymous functions for simple data value transforms. The anonymous functions are defined with the following syntax

sqr = @(x) x.^2;

I would like to have a simple anonymous function that returns more than one output that can be used as follows . . .

[b,a] = myAnonymousFunc(x);

The Matlab documentation suggests that this is possible, but it does not give an example of the syntax needed to define such a function.

http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162

What is the syntax to define such a function [in a single line, like the code example at the top of my post]?

Answers


Does this do what you need?

>> f = @(x)deal(x.^2,x.^3);
>> [a,b]=f(3)
a =
     9
b =
    27

With this example, you need to ensure that you only call f with exactly two output arguments, otherwise it will error.

EDIT

At least with recent versions of MATLAB, you can return only some of the output arguments using the ~ syntax:

>> [a,~]=f(3)
a =
     9
>> [~,b]=f(3)
b =
    27

If you'd rather not skip outputs using tilde ~ nor output a cell array, you'd only need an auxiliary anonymous function:

 deal2 = @(varargin) deal(varargin{1:nargout});
 myAnonymousFunc = @(x) deal2(x.^2, x.^3);

then you can obtain just the first output argument or both first and second one:

x = 2;
[b,a] = myAnonymousFunc(x)
b = myAnonymousFunc(x)

results:

b = 4

a = 8

b = 4


You can get multiple outputs from an anonymous function if the function being called returns more than a single output. See this blog post on the MathWorks website for examples of this in action.

There are two ways to get multiple outputs from an anonymous function:

  • Call a function which returns multiple outputs

    From the blog post linked to, they use the eig function like so

    fdoubleEig = @(x) eig(2*x)
    [e, v] = fdoubleEig(magic(3))
    
  • Alternatively you can construct an anonymous function which returns multiple outputs using the deal function.

    Here is one I made up:

    >>> f = @(x, y, z) deal(2*x, 3*y, 4*z)
    >>> [a, b, c] = f(1, 2, 3)
    
    a = 
         2
    b = 
         6
    c = 
         12
    

Edit: As noted by Sam Roberts, and in the blog post I link to, you must use the correct number of output arguments when using deal, otherwise an error is thrown. One way around this is to return a cell of results. For example

>>> f = @(x, y, z) {2*x, 3*y, 4*z}

>>> t = f(1, 2, 3)

>>> [a, b, c] = t{:}

    a = 
         2
    b = 
         6
    c = 
         12

Need Your Help

Dynamically replace the contents of a C# method?

c# methods assemblies cil swap

What I want to do is change how a C# method executes when it is called, so that I can write something like this:

insert the contents of a file to another (in a specific line of the file that is sent)-BASH/LINUX

linux bash shell

I tried doing it with cat and then after I type the second file I added | head -$line | tail -1 but it doesn't work because it performs cat first.