Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading functions

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

like image 367
Daniele Kerberos Avatar asked Dec 25 '11 18:12

Daniele Kerberos


Video Answer


2 Answers

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
like image 132
Andrey Rubshtein Avatar answered Sep 29 '22 17:09

Andrey Rubshtein


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

like image 25
Tom Avatar answered Sep 29 '22 16:09

Tom