【问题标题】:Pass function as argument to function将函数作为参数传递给函数
【发布时间】:2014-10-04 21:02:31
【问题描述】:

所以我有这两个组成的 .m 文件。这是我遇到的问题的一个例子,数学是伪数学

矩形.m:

function [vol, surfArea] = rectangle(side1, side2, side3)
vol = ...;
surfArea = ...;
end

ratio.m:

function r = ratio(f,constant)
% r should return a scaled value of the volume to surface area ratio 
% based on the constant provided.

% This line doesn't work but shows what I'm intending to do.
[vol,surfArea] = f

r = constant*vol*surfArea;
end

我不确定如何将矩形函数作为 f 传递,然后从 ratio 函数中访问 vol 和 surfArea。我已经阅读了关于函数句柄和函数函数的 Mathworks 页面,并且在弄清楚如何做到这一点时空手而归。我是 MATLAB 新手,所以这也无济于事。

如果您需要更多信息,请告诉我。

谢谢!

【问题讨论】:

  • 您可以将函数作为参数传递,例如在cellfun 中,但在此示例中,如果函数没有得到任何参数,您如何期望它给出结果?
  • 所以我正在(错误地)设想像这样传递的参数。 ratio(rectangle(1,2,3),2)。但是,我认为这更接近正确答案。 ratio(@rectangle,2) 或这个ratio(@(x)ratio(1,2,3),2)。在最后两种情况下,我不明白如何访问矩形函数的输出。
  • 你真的需要创建某种功能闭包吗?如示例所示,根本不需要函数句柄 - 只需让调用者首先调用它想要的任何f,然后将生成的volsurfArea 作为额外参数传递给ratio(即您的@987654331 @想法)
  • 您调用它的第一种方式只会返回rectangle 函数的第一个输出。同样,匿名函数只能返回一个输出。如果你真的需要传递函数(无论是通过它们的句柄还是在其他函数的参数中)并且你需要多个值,你必须将它们分组到一个结构中。

标签: matlab function argument-passing


【解决方案1】:

将函数rectangle作为ratio的参数传递的正确方法是

r = ratio( @recangle, constant )

然后您可以从ratio 中调用[vol,surfArea] = f(s1,s2,s3),但它需要知道sideX 参数。

如果ratio 不需要知道这些参数,那么您可以创建一个对象函数并将其作为参考参数传递。或者更好的是,您可以完全创建一个矩形类:

classdef Rectangle < handle

    properties
        side1, side2, side3;
    end

    methods

        % Constructor
        function self = Rectangle(s1,s2,s3)
        if nargin == 3
            self.set_sides(s1,s2,s3);
        end
        end

        % Set sides in one call
        function set_sides(self,s1,s2,s3)
            self.side1 = s1;
            self.side2 = s2;
            self.side3 = s3;
        end

        function v = volume(self)
            % compute volume
        end

        function s = surface_area(self)
            % compute surface area
        end

        function r = ratio(self)
            r = self.volume() / self.surface_area();
        end

        function r = scaled_ratio(self,constant)
            r = constant * self.ratio();
        end

    end

end

【讨论】:

  • 所以这是正确的。但是,我没有提到(并且没想到有人会得到)的一件事是我想继承到 ratio 函数中的一些矩形参数,以及我想从 ratio 函数中操作的其他参数。我会写下我今天的想法,但出于所有意图和目的,你的解决方案会很好用。
【解决方案2】:

虽然我没有在上面的问题中提出这个问题,但这就是我正在寻找的内容。

所以我想做的是将一些矩形参数传递给 ratio,同时能够从 ratio 函数中操作任意数量的矩形参数。鉴于我上面的 .m 文件,第三个 .m 看起来像这样。这个解决方案最终使用了MATLAB's anonymous functions

CalcRatio.m:

function cr = calcRatio(length)
% Calculates different volume to surface area ratios given
% given different lengths of side2 of the rectangle.
cr = ratio(@(x) rectangle(4,x,7); %<-- allows the 2nd argument to be 
                                  % manipulated by ratio function
end

ratio.m:

function r = ratio(f,constant)
% r should return a scaled value of the volume to surface area ratio 
% based on the constant provided.

% Uses constant as length for side2 - 
% again, math doesnt make any sense, just showing what I wanted to do.
[vol,surfArea] = f(constant);

r = constant*vol*surfArea;
end

【讨论】: