Difference between revisions of "OO Programming in Matlab"

From SUMOwiki
Jump to navigationJump to search
Line 2: Line 2:
  
 
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).
 
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).
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.
+
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.
 
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.

Revision as of 11:31, 1 February 2008

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'.

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). 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.

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 here.