scalar vs nonscalar structure: advantages?

39 Ansichten (letzte 30 Tage)
The Finnish Rein Deer
The Finnish Rein Deer am 10 Mär. 2020
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 Kommentar
Adam
Adam am 10 Mär. 2020
Bearbeitet: Adam am 10 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 10 Mär. 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 Kommentare
Steven Lord
Steven Lord am 11 Mär. 2020
The Finnish Rein Deer: if you get confused, you can always ask MATLAB.
S.Name = {'CLARK';'BROWN';'MARTIN'};
S.Gender = {'M';'F';'M'};
S.SystolicBP = [124;122;130];
S.DiastolicBP = [93;80;92];
isscalar(S)
ans =
logical
1
isstruct(S)
ans =
logical
1
If you ask if your other struct array isscalar it will tell you false (logical 0.)
isvector, iscolumn, and ismatrix all return true on the other struct array from your example, while isrow returns false.
Guillaume: The second is a structure array where each field is scalar.
To be pedantic, that's mostly but not completely correct. The Gender, SystolicBP, and DiastolicBP fields contain scalars. The Name fields contain char vectors. If you made them store string arrays you could make them scalar string arrays.
S(1).Name = 'CLARK'; % ' makes a char vector
isscalar(S(1).Name) % false
S(2).Name = "BROWN"; % " makes a string in recent releases
isscalar(S(2).Name) % true
The Finnish Rein Deer
The Finnish Rein Deer am 12 Mär. 2020
Thanks very much for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jakob B. Nielsen
Jakob B. Nielsen am 10 Mär. 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.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by