MATLAB Answers

scalar vs nonscalar structure: advantages?

69 views (last 30 days)
Hi,
What are the advantages/disadvantages of both typs of structures?
I am about to save my research participant data in Matlab but undecided about which type of structure to use.
Thank you,
TD
  1 Comment
Adam
Adam on 10 Mar 2020
I've never found a situation in which I'd need to choose between the two, to be honest. Putting aside the fact that I hate structures and use classes instead, since one is scalar and one is non-scalar that means, by definition, they are suitable for different scenarios. So even though it is obvious the only real advantage and disadvantage of the two is their property of being scalar or non-scalar and in most situations your data dictates whether you need something scalar or not! Both are still structures - basically like a christmas tree that you can just hang anything on and don't know what it is until you interrogate the struct.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 10 Mar 2020
What you want to do with your data? This documentation page gives two examples that show how each type of struct allows you to "slice" your data in a different way.
In the first example (the RGB image) accessing all the data in one color plane is easy while accessing all the data for a single pixel across the three planes is more involved.
In the second example (the patient data) accessing all the data for one patient is easy while accessing a particular piece of data for all patients is more involved.
I would like to point out one alternative that may be a better fit for your needs. If you have data arranged as a rectangular table consider storing it as a table array or a timetable array. If you look at the examples on the table documentation page you can see that you can access all data for a particular patient (in the "Specify Row Names" example) or all of a particular type of data for each patient ("Store Related Data Variables in Table") through one indexing expression.
  13 Comments

Sign in to comment.

More Answers (1)

Jakob B. Nielsen
Jakob B. Nielsen on 10 Mar 2020
There are probably people out there much smarter than me, so take this with a grain of salt.
If it were me, I would choose that my "main structure" should be a scalar structure - e.g. one simply called data. Inside this structure, you can then have multiple fields which can be other structures if you want, and each of these structures can be scalar or nonscalar as you choose. (You can also have cells, arrays, both of character and double, within all of these substructures). The primary advantage here is that a simple
save('researchdata.mat','data');
Saves all your stuff. As well as that your workspace is not cluttered, since it only has the one variable.

Tags

Products

Community Treasure Hunt

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

Start Hunting!