For loop using Struct data with multiple values per timepoint
5 views (last 30 days)
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
Stephen23
on 3 Dec 2022
Edited: Stephen23
on 3 Dec 2022
This would be a thousand times easier to answer if you had uploaded some sample data in the original format.
Instead of actually helping you, I will have to first try and replicate your data structure. Because your explanation is incomplete, I will assume that all 'IntegratedIntensity' and 'Timepoint' data are scalar numeric, and even then my structure might be wrong: much better would be, if you uploaded a sample data structure (which does not have to be all of it, just enough to work with).
As an aside, that data structure would be much much better as 1x1 structures containing 29890x1 numeric fields, rather than 29890x1 structures containing 1x1 fields.
% Fake data structure:
N = 29890;
S.DNA = cell2struct(num2cell([123*rand(N,1),randi(145,N,1)]),{'IntegratedIntensity','Timepoint'},2);
S.ABC = cell2struct(num2cell([123*rand(N,1),randi(145,N,1)]),{'IntegratedIntensity','Timepoint'},2);
S.XYZ = cell2struct(num2cell([123*rand(N,1),randi(145,N,1)]),{'IntegratedIntensity','Timepoint'},2)
S.DNA
% Get unique times:
iin = [S.DNA.IntegratedIntensity];
tpt = [S.DNA.Timepoint];
[uni,~,idx] = unique(tpt);
% Means and standard deviations:
allmeans = accumarray(idx,iin(:),[],@mean); % or GROUPSUMMARY
allstdev = accumarray(idx,iin(:),[],@std); % or GROUPSUMMARY
%
plot(uni,allmeans, uni,allstdev)
legend({'Means','Standard Deviations'})
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
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:
Seth Furman
on 7 Dec 2022
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")
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!