Page 1 of 1

Constructor methods in MATLAB

Posted: January 13th, 2009, 12:21 am
by Yura
I have a suspicion that MATLAB allows only one constructor method per class? Is that true?Here is how I arrived to this conslusion. I started defining a second constructor method per class, but MATLAB told me 'Class_X might be used incompatibly or redifined'. What does it mean?

Constructor methods in MATLAB

Posted: January 13th, 2009, 9:40 am
by Cuchulainn
QuoteOriginally posted by: YuraI have a suspicion that MATLAB allows only one constructor method per class? Is that true?Here is how I arrived to this conslusion. I started defining a second constructor method per class, but MATLAB told me 'Class_X might be used incompatibly or redifined'. What does it mean?Why don't you post the class interface?

Constructor methods in MATLAB

Posted: January 16th, 2009, 3:36 am
by Yura
The class interface is too large too post here and classified ;) Here is an example which leads to the same error messageclassdef Class_Xproperties a bendmethods function obj = Class_X(obj,a) obj.a = a; end function obj = Class_X(obj,a,b) obj.a = a; obj.b = b; endend

Constructor methods in MATLAB

Posted: January 16th, 2009, 6:30 am
by Cuchulainn
It seems indeed that multiple constructors are not allowed QuoteComparisons to other object-oriented languagesFor those familiar with other object-oriented languages such as C++ and Java, some of the features found in those languages will be noticeably absent:• Multiple constructors cannot be defined. The only method for defining different constructors is to check the number of arguments in the one and only constructor and invoke different behavior based on that clue. In this way one may initialize default values for arguments not specified in the original call.• Static properties are handled differently. One can use the persistent keyword to create a static variable in a static function, but there is no concept of a persistent property in the class definition.• Member methods require incessant dereferencing. The first argument of each member method is a handle to the class instance, usually called "obj". This might be viewed as the "this" pointer used in C++ and is required for accessing member data in member methods.• MATLAB is weakly typed and what method to call is not based on argument signature.So, you have to create a big 'catch-all' constructor with a large number of arguments. Since Matlab is weakly typed I reckon that this is an error-prone trick. Another trick is to define set(arguments) functions after the object has been created.hth//. property 'b' is not initialised!?. in general i would avoid 'obj' if possible (performance, safety)

Constructor methods in MATLAB

Posted: January 16th, 2009, 6:49 pm
by Yura
Thanks Chuch, for finding this! It's good to know for sure. I suspect that mathworks might introduce some changes to their OOP in the future to remove some of those bullet points that you quoted. OOP in Matlab is very young. I don't think you could call Object Oriented Programming that what was available in Matlab before 2008a version. As for the universal constructor method arguments, I'll use argvarin for passing arguments. It's a cell array... I'm sure you know....

Constructor methods in MATLAB

Posted: February 1st, 2009, 5:30 am
by Cuchulainn
Yura,This looks like a serious attemopt to use OO in Matlab