Difference between revisions of "OO Programming in Matlab"

From SUMOwiki
Jump to navigationJump to search
Line 1: Line 1:
Matlab's implementation of object oriented programming is curious to say the leastIf you have little experience with Matlab programming it is advised you [http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f6-39071.html 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'.
+
There are plenty of unusual and confusing aspects to Matlab object oriented programming, so we strongly advice potential developers to read the Matlab documentation on object oriented programming thoroughlyLuckily the situation has improved somewhat with the introduction of Matlab R2008. In any case, it is recommended you do some background reading on Object Oriented (OO) programming in Matlab, even if you are already familiar with an OO language.
  
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 good starting point is the tutorial available [http://www.mathworks.com/company/newsletters/digest/2008/mar/matlab_oop.html 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.method(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.
+
See also the FAQ entry concerning Matlab R2008 [[FAQ#Will_the_SUMO_Toolbox_work_with_matlab_R2008_and_later|here]].
 
 
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.
 
 
 
More documentation can be found [http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f6-39071.html here].
 

Revision as of 19:12, 19 April 2008

There are plenty of 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. Luckily the situation has improved somewhat with the introduction of Matlab R2008. In any case, it is recommended you do some background reading on Object Oriented (OO) programming in Matlab, even if you are already familiar with an OO language.

A good starting point is the tutorial available here.

See also the FAQ entry concerning Matlab R2008 here.