Is struct harmful for MATLAB projects?
Xiang Xu
on 6 Feb 2024
Latest activity Reply by David Young
on 11 Mar 2024
Struct is an easy way to combine different types of variants. But now MATLAB supports classes well, and I think class is always a better alternative than struct. I can't find a single scenario that struct is necessary. There are many shortcomings using structs in a project, e.g. uncontrollable field names, unexamined values, etc. What's your opinion?
16 Comments
For the reasons others have given - but particularly to avoid proliferation of m-files - I use structs whenever they are sufficient. Occasionally, though, it is neater to package up properties and methods together, and then I turn to classes.
However, there are situations when classes are more efficient. If your data has a tree structure you can build a representation from structs, but if you then need to update a value deep inside the tree this will be slow because you'll have to rebuild all of the ancestors. Handle classes allow this to be done efficiently - though at the cost of losing the consistency and safety of assign-by-value.
It's worth noting too that dictionaries offer another alternative to structs. They are more versatile and sometimes more efficient. There's some good discussion in the comments on this blog.
I find that OOP is often over-rated. In many use-cases there is no specific need for data encapsulation or security, and no data-specific methods that should be enclosed with the data (rather than being available as generic code functions). In such cases, structs are much easier to code, debug, use and modify than classes that require their own separate m-files. With structs, I can keep my focus in the active file, rather than constantly switching back-and-forth between m-files.
Here's a simple example: the EventData parameter of event callbacks used to be a simple struct with easily identifiable fields. Nowadays we need to define a new subclass whose only purpose is to store 2 simple data values. This is overkill in most cases. I have better ways to use my worktime, and find that using classes rather than structs doesn't provide any real benefit that's worth the extra work, debugging etc. I rather spend my worktime making my code work better, than making it look better.
Classes do make the code more structured and assist long-term maintenance. On the other hand, they also causes software bloat which has the oposite effect: 10 lines embedded within the main code are often more readable/maintainable than a separate 100-line class file. In my experience, well-written and well-structured procedural code is actually more readable/maintainable than a poorly-designed spaghetti of interconnected classes (which is too often the case with OOP code).
[Most] everyone have heard about Design Patterns and UML designs. But how many Matlab users actually use them in practice? In my experience, most users who use OOP do not...
My advice: any data structure that is just used to store data, certainly transient/temporary data, might be better served with a simple struct (or struct array) than classes.
load() of a .mat file produces a struct that has potentially unknown field names. It would be a nuisance to stop and convert the struct into a class at load point.
I did a search for class vs. struct in MATLAB; there are a few interesting results. I will post them as replies to this comment so that they can be commented on separately.
They server different purpose and are both equally useful.
A class has a fixed set of properties (baring the use of dynamic props) with associated methods which the user cannot modify. Indeed if what you want is a fixed interface with your user, then use a class.
A class cannot be created on the fly, it needs an m file to define it. If your code needs to return structured data whose fields are unknown a priori, use a struct. See readstruct for example. A struct is a lot more lightweight than a class.
Most of my code is OOP so I use classes a lot, some of these classes do use structs to interface with the user or internally.
Are cell arrays harmful as well? Structs and cells are a lot alike, except for the way they are indexed.
Also, classes require separate classdef files, which can be a bit cumbersome, especially when there aren't any methods you wish to attach.