Overloading functions

前端 未结 2 516
滥情空心
滥情空心 2020-12-03 01:38

Is there a way to have two functions with the same name but with different arguments inside the same class in Matlab?

相关标签:
2条回答
  • 2020-12-03 02:06

    The correct answer has already been given by Andrey. However, I've been running some experiments for some time now and I'd like to show what I think is another relatively straightforward way that has some benefits. Also, it's a method MATLAB uses for its built-in functions quite a bit.

    I'm referring to this kind of key-value pair way of passing arguments:

    x = 0:pi/50:2*pi;
    y = sin(x);
    plot(x, y, 'Color', 'blue', 'MarkerFaceColor', 'green');
    

    There are numerous ways of parsing a varargin cell array, but the cleanest way to do this that I've found so far uses the MATLAB inputParser class.

    Example:

    function makeSandwiches(amount, varargin)
    %// MAKESANDWICHES Make a number of sandwiches.
    %// By default, you get a ham and egg sandwich with butter on white bread.
    %// Options:
    %// amount       : number of sandwiches to make (integer)
    %// 'butter'     : boolean
    %// 'breadType'  : string specifying 'white', 'sourdough', or 'rye'
    %// 'topping'    : string describing everything you like, we have it all!
    
    p = inputParser();                          %// instantiate inputParser
    p.addRequired('amount', @isnumeric);        %// use built-in MATLAB for validation
    p.addOptional('butter', 1, @islogical);
    p.addOptional('breadType', 'white', ...     %// or use your own (anonymous) functions
        @(x) strcmp(x, 'white') || strcmp(x, 'sourdough') || strcmp(x, 'rye'));
    p.addOptional('toppings', 'ham and egg', @(x) ischar(x) || iscell(x))
    p.parse(amount, varargin{:});               %// Upon parsing, the variables are
                                                %// available as p.Results.<var>
    
    %// Get some strings
    if p.Results.amount == 1
        stringAmount = 'one tasty sandwich';
    else
        stringAmount = sprintf('%d tasty sandwiches', p.Results.amount);
    end
    
    if p.Results.butter
        stringButter = 'with butter';
    else
        stringButter = 'without butter';
    end
    
    %// Make the sandwiches
    fprintf(['I made you %s %s from %s bread with %s and taught you ' ...
        'something about input parsing and validation in MATLAB at ' ...
        'the same time!\n'], ...
        stringAmount, stringButter, p.Results.breadType, p.Results.toppings);
    
    end
    

    (slashes after comments because SO doesn't support MATLAB syntax highlighting)

    The added benefits of this method are:
    - Possibility to set defaults (like Python, where you can set arg=val in the function signature)
    - Possibility to perform input validation in an easy way
    - You only have to remember the option names, not their order as the order doesn't matter

    Downsides:
    - there may be some overhead that may become significant when doing many function calls and not much else (not tested)
    - you have to use the Results property from the inputParser class instead of using the variables directly

    0 讨论(0)
  • 2020-12-03 02:23

    In short : No, it is not possible.

    However, You can mimic this kind of behavior:

    Obviously, since Matlab is a dynamic language, you can pass arguments of any type and check them.

    function foo(x)
        if isnumeric(x)
            disp(' Numeric behavior');
        elseif ischar(x)
            disp(' String behavior');
        end
    end
    

    You can also use varargin, and check the number of parameters, and change the behavior

    function goo(varargin)
        if nargin == 2
            disp('2 arguments behavior');
        elseif nargin == 3
            disp('3 arguments behavior');   
        end
    end
    
    0 讨论(0)
提交回复
热议问题