How do I index within a structure?

63 visualizzazioni (ultimi 30 giorni)
Daniel Bridges
Daniel Bridges il 18 Feb 2016
Risposto: Mostafa il 15 Nov 2020
I am trying to take the mean of a structure.field column's number for multiple patients in a structure and store it for a plot of all the means later.
ShiftData(1).meanX = mean([patient(applicablepatients).Xtumorshift(1)])
This causes an error: Expected one output from a curly brace or dot indexing expression, but there were 65 results.
The MATLAB Help Documentation " Access Data in a Structure Array" states, "Note: You can index into part of a field only when you refer to a single element of a structure array. MATLAB® does not support statements such as S(1:2).X(1:50,1:80), which attempt to index into a field for multiple elements of the structure."
It appears that what I am trying to do is not supported. Am I understanding this correctly? Why is it not supported? Must I first store the data in a vector and then take the mean of that vector? Why?
Here is the contents (ID changed) of the second element in structure 'patient', patient(2):
ID: 999999
Xboneshift: [1x21 double]
Yboneshift: [1x21 double]
Zboneshift: [1x21 double]
Xtumorshift: [1x21 double]
Ytumorshift: [1x21 double]
Ztumorshift: [1x21 double]
TableLat: [1x21 double]
TableLong: [1x21 double]
TableVert: [1x21 double]
TxDate: {1x21 cell}
FxNumber: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]
(Is the correct terminology "structure(element).field"?)

Risposta accettata

Daniel Bridges
Daniel Bridges il 10 Mag 2016
Modificato: Daniel Bridges il 16 Mag 2016
Instead of indexing within an array stored in a structure element, I sorted the structure elements' arrays into a new structure, essentially David Sanchez's suggestion.
One can index into a structure field the same way one indexes into an array, if that field is an array. Working with ulrickls's dicomrt2matlab files, a DICOM-RT contour can be visualized:
scatter3(contours(9).Points(:,1),contours(9).Points(:,2),contours(9).Points(:,3))

Più risposte (2)

David Sanchez
David Sanchez il 18 Feb 2016
% dummy struct
your_struct.field_1 = rand(5,1);
your_struct.field_2 = rand(5,1);
% array with field-mean
your_means = structfun(@mean,your_struct);
your_means =
0.6307
0.5440
  3 Commenti
Daniel Bridges
Daniel Bridges il 19 Feb 2016
Modificato: Daniel Bridges il 16 Mag 2016
Thank you for telling me of structfun. However, I cannot use it directly because the structure contains a non-scalar field, namely, the treatment date.
Your second method is similar to what I implemented: I want to average "across" element fields, e.g. averaging the columns in your test field for all elements: 79 and 68, 75 and 70, 73 and 68. To this end I wrote this code:
for loop = 1:totalfractionnumber
for loop2 = 1:NumOfAppPat
shifts(loop).Xtumorshift(loop2) = patient(applicablepatients(loop2)).Xtumorshift(loop);
shifts(loop).Ytumorshift(loop2) = patient(applicablepatients(loop2)).Ytumorshift(loop);
shifts(loop).Ztumorshift(loop2) = patient(applicablepatients(loop2)).Ztumorshift(loop);
end
meanvectorx(loop) = mean(shifts(loop).Xtumorshift);
meanvectory(loop) = mean(shifts(loop).Ytumorshift);
meanvectorz(loop) = mean(shifts(loop).Ztumorshift);
stdvectorx(loop) = std(shifts(loop).Xtumorshift);
stdvectory(loop) = std(shifts(loop).Ytumorshift);
stdvectorz(loop) = std(shifts(loop).Ztumorshift);
end
However, it still appears less-than-ideal to have to create more vectors: It is not really using the data in the structure, but only copying from it.
Jason W
Jason W il 19 Lug 2016
I am wondering the same thing - the point of using structures was to help manage the large number of variables I had. Though dynamic indices has improved my code in storing values, I now need to create plots of those values and am forced to create many variables again.

Accedi per commentare.


Mostafa
Mostafa il 15 Nov 2020
I had the same question and I found the neatest solution in arrayfun. While structfun is useful for applying a function across fields, arrayfun lets us move across elements of one filed.
Considering the second example of David Sanchez:
your_means =arrayfun(@(x) mean(x.test), patient)
your_means =
75.6667 68.6667

Community Treasure Hunt

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

Start Hunting!

Translated by