1

I want to create an anonymous function which takes the first output variable of an existing vector function in file in Matlab. For example, the existing vector function in a separate M-file is

function [y1,y2] = myfun(x1)
    y1 = x1;
    y2 = x1^2;
end

Is it possible to create an anonymous scalar function from myfun() which takes the value of y1? Thank you for any suggestions.


P.S. I am doing this because actually my original function is more like

function [y1,y2] = myfun(x1,x2)
    y1 = x1+x2;
    y2 = x1^2+x2^2;
end

and I want to create a scalar function y1 with only one parameter x1 (pass a known value of x2 to the anonymous function).

Chang
  • 11
  • 5
  • Wouldn't `@(x1) myfun(x1)` work for the first case, and `@(x1) myfun(x1,x2)` for the second? – David Sep 24 '15 at 03:15
  • then the new function still returns two values. Is it possible for it just to return y1 or y2? – Chang Sep 24 '15 at 15:03
  • Well I tried my suggestion and it just returned `y1`. It's harder to get it to return `y2`. Maybe look at this: http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it and do `paren=@(x,varargin) x(varargin{:})` then `fun=@(x1) paren(myfun(x1,x2),1)` to get `y1` and `fun=@(x1) paren(myfun(x1,x2),2)` for `y2`. I haven't tested it, but if it works, add it as an answer to this question. – David Sep 24 '15 at 23:20
  • Is [this question](http://stackoverflow.com/q/3096281/52738) related to what you want to do? – gnovice Sep 25 '15 at 17:18
  • @David, thanks for the solution, it works like a charm. The only adjustment I need to make is to combine y1 and y2 into a vector, otherwise the anonymous function just takes y1 as output – Chang Sep 26 '15 at 15:18

2 Answers2

0

I think I know what you're looking for, but it's a little unclear. If you're starting with a function like this:

function [y1,y2] = myfun(x1,x2)
  y1 = x1+x2;
  y2 = x1^2+x2^2;
end

You can make a wrapper anonymous function with a fixed value of x2 (fixed at whatever the variable is when the anonymous function is created) like this:

newFcn = @(x1) myfun(x1, x2);

Now, you can use this to get whichever of the two outputs from myfun you want. For example:

y1 = newFcn(x1);        % Gets the first output
[~, y2] = newFcn(x1);   % Gets the second output
[y1, y2] = newFcn(x1);  % Gets both
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • This does not really solves my problem but the question you pointed to is related. The other answer to that question has the same spirit as the one provided by David and it works well for me. Thanks. – Chang Sep 26 '15 at 15:29
0

Based on @David 's method I adjusted my code and it works well.

function y = myfun(x1,x2)
    y1 = x1+x2;
    y2 = x1^2+x2^2;
    y = [y1 y2];
end

and the anonymous functions output1 and output2 return y1 and y2 respectively.

paren=@(y,varargin) y(varargin{:});
output1 = @(x1) paren(myfun(x1,x2), 1);
output2 = @(x1) paren(myfun(x1,x2), 2);
Chang
  • 11
  • 5