フィルターのクリア

passing whole function expression as function argument

3 ビュー (過去 30 日間)
buscuit
buscuit 2015 年 11 月 19 日
コメント済み: buscuit 2015 年 11 月 24 日
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

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 19 日
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 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 23 日
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
buscuit
buscuit 2015 年 11 月 24 日
Walter, thank you very much for all your feedback

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by