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?
David Young
David Young on 11 Mar 2024
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.
Yair Altman
Yair Altman on 6 Feb 2024
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.
Walter Roberson
Walter Roberson on 6 Feb 2024
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.
goc3
goc3 on 6 Feb 2024
Some third-party apps also return structs or struct arrays. Being able to directly use these without conversion is helpful.
goc3
goc3 on 6 Feb 2024
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.
goc3
goc3 on 6 Feb 2024 (Edited on 6 Feb 2024)
"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."
goc3
goc3 on 6 Feb 2024
One person claims: "A struct is just a christmas tree that you can hang anything off at any time you want. Very nice and convenient, but can be a nightmare for debugging, sharing code amongst others, readability, etc etc because just by looking at a piece of code you have no idea what fields are on a struct at any given moment unless it is entirely defined and limited to the scope you are looking at."
A response to that statement: "Just looking at a class object you often have a pretty poor idea of what the properties are unless you read the class definition in detail. There are properties, there are hidden properties, there are methods that look like properties, the display method can result in output that gives a fairly different interpretation of the object then what is actually present in the properties, and what looks like a straight-forward assignment can end up executing code that changes dependent properties or ends up computing something that changes program state in non-obvious ways. If you want to understand what your program is doing, do away with the OOP and stick with struct."
Guillaume
Guillaume on 6 Feb 2024
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.
Matt J
Matt J on 6 Feb 2024 (Edited on 6 Feb 2024)
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.
goc3
goc3 on 6 Feb 2024
In the past I have used cell arrays quite a bit, but I now find structs to be much better than cell arrays, at least, for my projects.
As a long-time MATLAB user that is very comfortable with procedural programming and aspects of functional programming, but not with OOP, I am curious to see what others have to say on this matter.

Tags

No tags entered yet.