MATLAB Answers

scalar vs nonscalar structure: advantages?

조회 수: 69(최근 30일)
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,
  댓글 수: 1
Adam 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 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
The Finnish Rein Deer
The Finnish Rein Deer 12 Mar 2020
Thanks very much for your help.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Jakob B. Nielsen
Jakob B. Nielsen 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
Saves all your stuff. As well as that your workspace is not cluttered, since it only has the one variable.



Community Treasure Hunt

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

Start Hunting!

Translated by