|
|
(8 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
− | Matlab's implementation of object oriented programming is curious to say the least. If you have little experience with Matlab programming it is advised you read through the documentation first. Since, while programming classes in matlab can take some getting used to, debugging object oriented programs can be hell. In Matlab each class has to be in its own directory and the directory name needs to be prefixed with a '@'. Private functions are placed in a subdirectory 'private'. | + | We strongly advice potential developers to read the Matlab documentation on object oriented programming thoroughly, even if you are already familiar with an OO language. |
| + | A good starting point is the tutorial available [http://www.mathworks.com/company/newsletters/digest/2008/mar/matlab_oop.html here]. |
| | | |
− | Each method needs its own m file and if a member function changes the object it was called on you must remember to pass the object as a return value as well (in the toolbox code this object is often called 's' or 'this')! Matlab does not support pass by reference, not for primitive types and not for objects. Method invocation on an object is also different from other languages. Eg: result = myMethod(object, someArgument). And if the object is being changed the syntax should be: [result object] = myMethod(object, someArgument).
| + | A starting point for Object Oriented Programming in general [http://en.wikipedia.org/wiki/Object_oriented can be found here]. |
− | However, to make things confusing, if you are invoking a method on a Java object (Java is seamlessly integrated into Matlab) you follow the Java syntax: result = object(someArgument). Also, Java objects are always passed by reference, in contrast to Matlab objects, which never are. Keep this in mind.
| |
− | | |
− | By default Matlab does not allow non-member functions to access or change the datamembers of an object. If you want to make this possible you need to provide a subsref.m file (for access) and/or a subassgn.m (for assignment) file in the class directory. Writing and debugging these files can be a real pain and we suggest keeping them simple or simply avoiding them. To get started you can just use a copy of the subsref/subassgn files from an existing model type. These should work well for most cases.
| |
− | | |
− | Finally two notes about subclassing. Say you have a base class Base and a derived class Derived which has Base as its parent. If, in Matlab you create an object of type Derived matlab will automatically add a datamember called "Base" to the resulting object. Also, unlike other languages you can not create an array of type Base and fill it with different Derived objects. All objects in a matlab array MUST have exactly the same type!! If this is not the case Matlab will automatically convert all objects to the same type WITHOUT warning!!! If you do need to put multiple objects of different types into the same array (without resorting to cells) you will have to wrap them in a common class. This is also the reason we have provided a WrappedModel class.
| |
− | | |
− | There are plenty of other unusual and confusing aspects to Matlab object oriented programming, so we strongly advice potential developers to read the Matlab documentation on object oriented programming thoroughly.
| |
Latest revision as of 19:26, 29 September 2009
We strongly advice potential developers to read the Matlab documentation on object oriented programming thoroughly, even if you are already familiar with an OO language.
A good starting point is the tutorial available here.
A starting point for Object Oriented Programming in general can be found here.