Multiple class constructor Matlab

18,224

Solution 1

Each class has one constructor. However ... the constructor can accept any number and type of arguments, including those based on varargin.

So, to provide the option of a default third argument in Java you could write something like this (examples based on java documentation):

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}
public Bicycle(int startCadence, int startSpeed) {
    gear = 1;
    cadence = startCadence;
    speed = startSpeed;
}

In Matlab you could write

classdef Bicycle < handle
    properties (Access=public)
        gear
        cadence
        speed
    end
    methods (Access = public)
        function self = Bicycle(varargin)
            if nargin>2
                self.gear = varargin{3};
            else
                self.gear = 1;
            end
            self.cadence = varargin{1};
            self.speed = varargin{2};
        end
    end
end

Solution 2

Each class has only one constructor, and each .m-file can only contain one class definition.

If you want to have a class with slight differences depending on input, you can use properties that define switches that are recognized by the class methods. If you want to have very different classes depending on input, you can create a generateClass-function that will call either one or the other class defined in different files. Of course, if these different classes have lots of common methods and properties, you can create both as subclasses to a common superclass.

Solution 3

The answer of Pursuit works, but a user not familiar to the function can't see how many arguments are needed or what they are for. I would recommend this:

methods (Access = public)
    function self = Bicycle(startCadence, startSpeed, startGear)
        if nargin>2
            self.gear = startGear;
        else
            self.gear = 1;
        end
        self.cadence = startCadence;
        self.speed = startSpeed;        
    end
end

If you now type "Bicycle(" and wait you can see at least the three arguments. The second possibility is not shown though. It seems possible (e.g. for plot) but I don't know how to do this.

Solution 4

No. The constructors in OOP matlab are very restricted compared to other languages. It is not explicitly stated in the documentation AFAIK that you can have multiple constructors, but it refers to the constructor of a class in the singular throughout the documentation.

https://www.mathworks.com/help/matlab/matlab_oop/class-constructor-methods.html

Share:
18,224

Related videos on Youtube

bsmca
Author by

bsmca

Updated on June 04, 2022

Comments

  • bsmca
    bsmca almost 2 years

    Is it possible define more than one class constructor in Matlab? If yes, how?

  • learnvst
    learnvst about 12 years
    Good post. . . but note to the original poster: Be really REALLY careful with inheritance. It gives you lots of rope to hang yourself with. Do not resort to it if you just want to have a default constructor and other constructors that can initialize variables. Use varargin to get this functionality.
  • bsmca
    bsmca about 12 years
    Same class, just the process of initialization is different. I think I'll pass all the parameters required to build an instance in these two ways, and one more parameter which will define the way to build. Dirty but quick.. sad, my solution was getting so understandable.. shame on you matlab!
  • Jonas
    Jonas about 12 years
    @bsmca: You can always define (static) methods that implement the two different ways to build, and then you call either of them from the common constructor. Just make sure that you only create the object either before or after, but not inside these methods.
  • Jonas
    Jonas about 12 years
    Note that you cannot create "self" inside an if-statement, so the first time you assign anything to "self" has to come before or after the if-clause.
  • bsmca
    bsmca about 12 years
    ended up with this solution :) tnx!
  • brown.2179
    brown.2179 about 9 years
    In MATLAB I often find myself using static Factory method pattern or two stage initialization pattern if a lot of flexibility is needed in the constructor.
  • Matt Krause
    Matt Krause over 8 years
    Does that actually work for you? I get a "not enough input arguments errror" (MATLAB:minrhs) if I don't provide all of the arguments (on 2014a and b).This does seem to work on "free" functions, but not constructors/class members.
  • Pursuit
    Pursuit over 5 years
    I've updated the code sample and re-tested it, based on a (correct by rejected) edit from a low rep user. Also added enough boilerplate to generate a complete file, which should be named named "Bicycle.m". Tested in R2016b.