passing whole function expression as function argument

hi everyone,
I am trying to create a function that has as an argument another function expression. I can manage to do this:
function [out1] = my_func(in1).
Then I can use a whole function expression as in1 like this:
[out1] = my_func( another_func() ).
So far so good.
My problem arises when the function another_function() has more than one outputs. So when another_func() is something like this:
function [out2 out3] = another_func(in2)
then [out1] = my_func( another_func() ) will only return the first of the outputs of another_func() which is out2.
Is there any way to use the expression [out1] = my_func( another_func() ) and get all outputs of another_func()?
thank you in advance for your help

 Accepted Answer

Yes, but only in a very restricted context.
If you have an anonymous function A that effectively just calls another function B and B returns multiple outputs, then those multiple outputs will be returned from A.
For example,
my_anonymous_func = @() another_func(in)
then because another_func returns multiple outputs, they will all be returned from my_anonymous_func
I do not know if this is strictly the only sequence it will work in: it might also be possible in the form
function [varargout] = my_func(in1)
[varargout{1:nargout}] = another_func(in1);

5 Comments

Hi Walter,
thanks a lot for the input. The another_func() and my_func are both non-anonymous functions.
I tried to directly extract the outputs from another_func() and assign them in variables inside my_func() which I guess is what you are suggesting on your last piece of code but it won't work.
Furthermore the number of outputs of another_func() is known (two) so I guess I do not need to use the varargout approach (sorry, I forgot to mention that).
I am surprised that I am actually able to access the first output of the another_func() function this way but not its second one using this method...
I am running out of ideas ...
Just explicitly assign the outputs
function out1 = my_func(in1);
[out2, out3] = another_func(in1);
now do something that calculates out1
You will not be able to program something that would look like
function out1 = my_func(in2, in3)
...
and call it as
my_func(another_func(in1))
hoping to have the two outputs of another_func captured as the two inputs in2 and in3 .
Remember, you can use
function out1 = my_func(func_in)
[out2, out3] = func_in();
now do something that calculates out1
and call it as
my_func(@() another_func(in1))
and you can even use
function out1 = my_func(in1, in2)
if nargin == 1 && isa(in1, 'function_handle')
[in1, in2] = in1();
end
now do something that calculates out1
Hi Walter,
for for your comments. They have been a great help so far.
In the examples of your last comment can the another_func be of an arbitrary name? Or it has to be a specific function?
My aim is to create a function that uses as inputs two other functions. Those functions will be matlab filters e.g. butter(), cheby1 etc... So I would like if the user was able to input different filters in that function.
cheers
The particular form
my_func(@() another_func(in1))
can use any function provided that in1 (or the other parameters) is known before the call to my_func .
If you want a function that takes two functions as inputs, then for example
function out = my_func(data, func1, func2)
out = func2(func1(data));
which could be called as
y = my_func(x, @butter, @cheby1)
The key is passing function handles
Walter, thank you very much for all your feedback

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Nov 2015

Commented:

on 24 Nov 2015

Community Treasure Hunt

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

Start Hunting!