Should I use object oriented programming in MATLAB?

19 views (last 30 days)
I am designing a large Hybrid Electric Vehicle model and simulation in MATLAB (multi-order dynamic models of 10+ vehicle components, etc). The intention is to run Monte Carlo simulations with the simulation using the Parallel Computing Toolbox in MATLAB on a 200 node cluster. The simulation was originally implemented in Simulink, but needed to be rewritten in MATLAB M-code for cost/availability/performance/distributed computing reasons.
I have done a large portion of the design and implementation with MATLAB's object oriented programming constructs (and it is an extremely beautiful and elegant solution, if I may say so myself). However, I am concerned that there may be a performance hit for doing it this way, and would like to know what others think about this decision that have done similar things using MATLAB's OO constructs. I would like to hear what others think before I start the next phase of implementing such a system on the cluster (in the case that there is an overwhelming consensus to serialize the code for this application).

Accepted Answer

Adrien Leygue
Adrien Leygue on 4 May 2011
I have recently had to rewrite 90% of our research code (model reduction) which had become too messy and too intricate to support further algorithmic developments and I naturally tried to switch to OO. For this application, performances were really critical.
I have quickly moved away from OO in Matlab and gone back to a more traditional style as the code became horribly slow (even after careful profiling and optimization), apparently due to some overhead in accessing object's methods and properties.
As Matlab has become more efficient in memory management (e.g. function arguments are not duplicated if they are not modified in the function), I prefer to use some structures to organize the data. These structures are passed as arguments to the several functions.
The only one place where I had to use OO was to benefit from the handle class for some data structure which was sometimes modified by some functions and was too big to even consider duplicating it.
In a nutshell: If Matlab had better OO performances, I'd recommend switching to OO, even for critical parts (where speed is critical) of the code. Since this is not the case I prefer to stick to structures sometime with function handle to mimic object methods. The only thing missing is the handle class inheritance.
NB: I do not claim to be an expert in Matlab OO programming, so if some OO guru has hints on how tweak things to speed up OO in order to make it as fast as traditional structures (in terms of accessing, and modifying the object properties + calling methods), I'll take all advices.
Hope this helps.

More Answers (1)

Jan on 4 May 2011
Program time = programming time + debug time + run time
Your goal is to get the results as fast as possible considering your limited budget. While it is very easy to spend 400 hours for programming, it will be hard to gain 400 hours less runtime.
There are a lot of opinions about the efficiency of OO programming. But for a reliable decision the above formula is more scientific.
Another formula:
Program cost = programming time * HWP +
debug time * HWFP +
run tim * HMC
HWP = Hourly wages for a programmer
HWFP = Hourly wages for a frustrated programmer
HMC = Hourly maintenance costs for the computers

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!