Best reason to replace structures with classes

67 views (last 30 days)
I use structures a lot. For example I might find the statistics of data contained in a structure containing measurements of ocean waves,
ndbc.timeGmt
ndbc.waveHeight
ndbc.wavePeriod
What would improve if I used classes instead of structures? For example would it make my code more transparent to another programmer? Help me decide if it's worth my time to (a) learn how to use classes myself, and (b) explain them to others using my code who are unfamiliar with classes as well.

Accepted Answer

David Young
David Young on 23 Jan 2012
I find OOP helpful in a situation where I'm computing something that depends on lots of different inputs - parameters, data, options or whatever - and where there are intermediate results that depend on some of these inputs but not on others. If only some of the inputs change between calls and I want to save intermediate results, it becomes complex to manage by passing structures around.
With OOP, it's simpler: the object can retain all the intermediate results, as values of private or protected properties. If I want to change one input, I call an appropriate method with just the new data, and it can access all the previous results. Different objects can hold different sets of results without any confusion.
An example is computing motion from an image sequence. This process typically involves lots of parameters, and at least two images. With OOP you can create an object that knows all the parameter settings and useful intermediate results like feature positions, and whose update method can be invoked with just one new image as an argument to recompute the motion efficiently. The code for this is here.
This aspect of OOP is really only a convenient way to create closures (functions with data frozen into them) which you can also do with nested functions in recent MATLAB. However, the OOP syntax is probably clearer and easier to use, and as others have said, there are other advantages too.

More Answers (3)

Sean de Wolski
Sean de Wolski on 23 Jan 2012
Using OOP can really help "idiot-proof" your code if well written. That is, you can make sure conditions are met before setting properties, you can conceal things you need (such as flags or handles). If you're going to have others using the code, I think this is a good reason to lean toward OOP. It just makes preventative programming easier.
As far as others being able to understand the classes: If you document them well and organize them well, it should be just as easy to follow as standard code (after a syntax learning curve).
My $0.02. (Ps. I just started using classes about a month ago, so I still have a lot of mountain to climb)

Jose Marcelo Duarte
Jose Marcelo Duarte on 20 Jan 2016
You can use OOP paradigm with structures too. I have recently converted some of my code from object to struct in order to compile individual methods into MEX functions (MatLab Coder v2015a do not accept class files as entry point). In my case, I basically only have to convert all methods, including the constructor, in functions and put them in separated files.
The main advantages of using classes for OOP instead of structures are code organization and protection against coding errors. This come with a cost in performance.
A brief explanation about class and object for people familiar to structure is "An object of a certain class is a structure that has a set of predefined functions to operate with it. The definition for both, the structure type and the set of function, is in its class file".

Andrew Newell
Andrew Newell on 23 Jan 2012
There are a lot of potential advantages to object-oriented programming. I can hardly do them justice in a short answer. Try reading Why use object-oriented design.
  2 Comments
K E
K E on 23 Jan 2012
This jumped out at me, "as the number of functions becomes large, designing and managing the data passed to functions becomes difficult and error prone. At this point, you should consider moving your MATLAB programming tasks to object-oriented designs."
Complicated data analyses do tend to produce a 'nest' of functions which are hard for another programmer to detangle. If classes could help someone else figure out the program flow, they might be worth it. Would love someone's confirmation that this is so.
Andrew Newell
Andrew Newell on 23 Jan 2012
I use them a lot. There is definitely a learning curve, but I think it is worth the effort if you are creating a large program.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!