MATLAB Answers

Multiple outputs from anonymous function

52 views (last 30 days)
Morten Nissov
Morten Nissov on 10 Dec 2019
Commented: Guillaume on 10 Dec 2019
I have a function of the following form
function [out1, out2] = demo_fcn( in )
out1 = in(1);
out2 = in(2);
which gets called by
[out1, out2] = @(x) demo_fcn(x);
but anonymous functions are not allowed more than one outputs. This is clearly a simplified example, the application is for a nonlinear programming problem where out1 is the objective function and out2 is the gradient calculation. I am not sure how I can structure this differently or in a way which is acceptable by MATLAB syntax.
Note the error messge is
Only functions can return multiple values.


Stephen Cobeldick
Stephen Cobeldick on 10 Dec 2019
"...which gets called by [out1, out2] = @(x) demo_fcn(x);"
But what you show is not calling any function: that line appears to be a syntactically-incorrect anonymous function definition... but it certainly does not call any function.
Is that really the code that you are trying? If so, then it appears that you might be getting confused between defining the function handle and calling the function handle.
"...but anonymous functions are not allowed more than one outputs."
Who told you that? Two output arguments worked perfectly when I tried just now:
>> fun = @(x) size(x);
>> [R,C] = fun(ones(2,3))
R =
C =
Morten Nissov
Morten Nissov on 10 Dec 2019
I mean the error message did, but I can see i may have misunderstood it.
The purpose of the code is that I need to calculate an objective function and it's gradients with respect to an unknown value "u' such that the optimizer can solve for this "u".
For example my earlier implementation when I only required the objective function and not the gradients was
objfun = @(x) get_obj(x)
I guess the problem is I'm not quite sure how to adapt the previous version above to also create and output the gradients.
Edit: The intention is to use this in conjunction with fmincon by the way.
Guillaume on 10 Dec 2019
So, I'm a bit unclear on what you are asking.
As pointed out by Stephen, anonymous functions can return more than one output (as long as the function delegates the actual processing to a function that returns more than one output).
Yes, some functions such as your demo_fun can't be implemented as an anonymous function since it's made of two statements and anonymous functions in matlab are limited to one non-branching statement. However, you're never forced to use anonymous functions, they're just syntactic sugar that can always be replaced by named functions. You can pass a handle to your demo_fun to fmincon and others, so why can't you use demo_fun as you have written it?

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 10 Dec 2019
One option is to have the two outputs to one vector, then separate them in a subsequent assignment:
demo_fcn = @(in) [in(1) in(2)];
in = rand(2,1)
Out = demo_fcn(in)
Out1 = Out(1)
Out2 = Out(2)
This works, however I cannot tell if it does what you want it to do.


Morten Nissov
Morten Nissov on 10 Dec 2019
This doesn't quite do what I want, I should maybe explain the problem a little more clearly.
The demo_fcn in question is meant to be calculating the objective function as well as its gradients for the unknown variable x such that it can be solved using a nonlinear minimizer like fmincon.
A more concrete example would probably have been
psi = @(u) objfun(u)
function [ psi ] = objfun( u )
psi = sum(u.^2)
where this is the original function and I would like to expand it to also return a calculation for the gradients of psi, resembling
[psi, grad_psi] = @(u) objfun(u)
function [ psi,grad_psi ] = objfun( u )
psi = sum(u.^2)
grad_psi = diff(psi,u); % poor example of gradient fcn; but same purpose
Star Strider
Star Strider on 10 Dec 2019
The approach I used would clearly not allow concatenation such as that unless the outputs of the two sub-functions were in cell arrays. That adds the additional complication of recovering the double array from the cell array, however that is not diffcult.
I encourage you to experiment with that approach.

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!