For loop using Struct data with multiple values per timepoint
Show older comments
Hello,
I am new to matlab so bear with me:
I currently have a 1x1 struct called "XY11" that contains 3 fields "DNA", "EGFRN" and "EGFRC" which are each 29890x1 structs with multiple fields. Particularly, I want to extract the "Integrated Intensity" and "Timepoint" fields, so I can plot the intensity of my objects over time.
Problem is, there are about 200-300 intensities per timepoint. There are 145 timepoints. I've written code to identify and average the intensities for a given timepoint, but I don't know how to structure the For loop so that each of my 145 timepoints is seperated into their own structure. See the code below.
% Identify all timepoints in the DNA struct
time = [XY11.DNA.Timepoint];
% Identify all intensities in the DNA struct
intensity = [XY11.DNA.IntegratedIntensity];
% Set time == 1 to evaluate intensities found in the first timepoint out of 145
timepoint1 = time == 1;
% Create a structure containing all of these timepoints
% Essentially a Struct containing only the number '1', but it helps me keep my timepoints organized
str.time = time(timepoint1);
% Create a structure containing all of the intensities found at timepoint 1
str.intensity = intensity(timepoint1);
% Find the mean of time (1, or whatever time ==) and intensity
means = structfun(@mean, str, 'uniform', 0);
% Find the STD of time (0, because it is unchaning) and intensity
stds = structfun(@std, str, 'uniform', 0);
The output of this code, Struct "str" is attached.
I don't know how to do the "For j = 1:num" thing, because I don't know how to define num. In my XY11.DNA structure there are hundreds of intensities per timepoint, so I'm not sure how to get matlab to know I need it to loop through all 145 timepoints. Maybe I'm just having "writer's block".
Your assistance is greatly appreciated.
Accepted Answer
More Answers (2)
Eric Delgado
on 3 Dec 2022
I don't know if I understand your issue, but...
uniqueTimePoints = unique(XY11.DNA.Timepoint);
for ii = 1:numel(uniqueTimePoints)
idx = XY11.DNA.Timepoint == uniqueTimePoints(ii);
str(ii).time = uniqueTimePoints(ii);
str(ii).intensity = intensity(idx);
str(ii).mean = mean(str(ii).intensity);
str(ii).std = std(str(ii).intensity);
end
4 Comments
Ryan Hecksel
on 3 Dec 2022
Eric Delgado
on 3 Dec 2022
What’s the class of XY11.DNA.Timepoint?
Ryan Hecksel
on 3 Dec 2022
Stephen23
on 3 Dec 2022
"but for some reason, uniqueTimePoints = unique(XY11.DNA.Timepoint) return an error: "
It returns an error because Eric Delgado's code provides 29890 inputs to UNIQUE, and is equivalent to doing this:
unique(XY11.DNA(1).Timepoint, XY11.DNA(2).Timepoint, .. , XY11.DNA(29890).Timepoint)
Clearly that is not going to do anything useful for you. As the original question shows, using concatenation is one way to handle that comma-separated list correctly. See also:
Alternatively, you can convert your struct into a table using struct2table.
load str.mat
t = struct2table(str)
varNames = t.Properties.VariableNames;
t = varfun(@transpose,t);
t.Properties.VariableNames = varNames
Then, you can use groupsummary to get the mean for each unique time.
tSummary = groupsummary(t,"time",["mean","std"])
stackedplot(tSummary,["mean_intensity","std_intensity"],Marker="*",MarkerEdgeColor="red",XVariable="time")
Aside: You can also convert the time variable to the appropriate duration or datetime if desired.
tSummary.time = seconds(tSummary.time)
tSummary = table2timetable(tSummary)
stackedplot(tSummary,["mean_intensity","std_intensity"],Marker="*",MarkerEdgeColor="red")
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

