0

I have a function that returns 2 things (e.g. a function value and a jacobian).

function [f_val, J_val] = f(x)
   f_val = x;
   J_val = [1 0; 0 1];%.....
end

What I want is a function to extract the first or the second output parameter. Clearly I can go [f_sol; J_sol] = f([1 1]) and then extract the appropriate value, but I need to do this inside of an anonymous function where that sort of assignment isn't possible.

Ugly Solution One can create the following function,

function out = output_part(i, f, varargin) %This calls it here, but I would rather not
    [all_out{1:nargout(f)}] = f(varargin{:});
    out = all_out{i};
end

And then call something like: output_part(2, @f, [ 0 1]) serving my purpose, but it is ugly. Better syntax would be output_part(2, f([0,1])) but I can't figure out how to write this. What we would really need is a way to have the multiple variable outputs of a function end up as variable inputs of the calling function. I can't figure out if that is possible.

Multiple Output Arguments: This is not duplicate of How can I index a MATLAB array returned by a function without first assigning it to a local variable? This is of the same nature, but multiple arguments are handled differently. Something a little more related is the kthout function in How to elegantly ignore some return values of a MATLAB function? which does something like my output_part above.

The problem here just to be that you cannot easily chain multiple outputs of a function into the multiple inputs of another function through function composition.

11
  • Possible duplicate of How can I index a MATLAB array returned by a function without first assigning it to a local variable? Commented Nov 10, 2015 at 1:07
  • 1. why don't you include an additional input argument to indicate which output you want, first or the second? 2. This and This should be useful to you. Commented Nov 10, 2015 at 1:25
  • @David Thanks. Alas, I don't think this is a duplicate as multiple return values are treated very different in matlab, and seem not to play nice with function compositions. Commented Nov 10, 2015 at 3:57
  • @ParagS.Chandakkar Thanks. (1) Alas, I can't modify that original function. Obviously I can write wrappers around it that emulate what you are talking about, but they are ad-hoc and ugly . (2) Thanks for the link to: stackoverflow.com/questions/7921133/… It is very similar, and it looks like the one I posted as the "ugly solution" is a generalization of the posted solution. The difference is that I can't just use nullary functions as they do. Commented Nov 10, 2015 at 3:59
  • I don't know what prevents you from modifying the original function. Otherwise, I don't know any other solution apart from those I have already listed. Commented Nov 10, 2015 at 4:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.