Plot data from nested structure
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi.
I have a nested structure with multiple participants (n=17), multiple datasets, and multiple phases segmented out of each trial, ie:
metrics.sub(i).EDA(1,1) is the data for all participants for the mean of a time series from stage 1 for each participant.
There are six stages in each trial and I want to create a single figure with 6 boxplots comparing the mean of all participants across the 6 stages. So, this would be the data for each of the six boxplots:
metrics.sub(i).EDA(1,1);
metrics.sub(i).EDA(1,2);
metrics.sub(i).EDA(1,3);
metrics.sub(i).EDA(1,4);
metrics.sub(i).EDA(1,5);
metrics.sub(i).EDA(1,6);
I'm having trouble efficiently creating an array (or any other format the boxplot(x) will handle). Right now, my poor coding skills has me using four for loops for each metric and creating a new field in the structure for each phase of each trial. I need to create many of these plots.
Is there any way I can do this with a single for loop?
Sincerely,
Bungling in Connecticut
2 Commenti
Risposta accettata
Voss
il 11 Dic 2023
Modificato: Voss
il 11 Dic 2023
load Proj_metrics
Something like this may have been what you intended:
Nparticipants = numel(metrics.sub);
%Create a 3D matrix to plot features across conditions
boxplotmatrix = NaN(4,Nparticipants,6);
for nsub = 1:Nparticipants
[nactivity,nindex] = size(metrics.sub(nsub).EDA);
boxplotmatrix(1:nindex,nsub,1:nactivity) = permute(metrics.sub(nsub).EDA,[2 3 1]);
end
%Create all boxplots with one for loop
figure
Nindices = size(boxplotmatrix,1);
NameofIndex = "Index "+(1:Nindices);
for nindex = 1:Nindices
subplot(Nindices,1,nindex)
boxplot(permute(boxplotmatrix(nindex,:,:),[2 3 1]));
title(NameofIndex{nindex})
end
Something like this may be useful as well:
Nparticipants = numel(metrics.sub);
measures = fieldnames(metrics.sub);
Nmeasures = numel(measures);
siz = zeros(Nparticipants,2,Nmeasures);
for mm = 1:Nmeasures
for ii = 1:Nparticipants
siz(ii,:,mm) = size(metrics.sub(ii).(measures{mm}));
end
end
max_siz = max(siz,[],1);
C = cell(1,Nmeasures);
for mm = 1:Nmeasures
%Create a 3D matrix to plot features across conditions
boxplotmatrix = NaN(max_siz(1,2,mm),Nparticipants,max_siz(1,1,mm));
for ii = 1:Nparticipants
boxplotmatrix(1:siz(ii,2,mm), ii, 1:siz(ii,1,mm)) = permute(metrics.sub(ii).(measures{mm}), [2 3 1]);
end
C{mm} = boxplotmatrix;
end
%Create all boxplots
figure('Position',[10 10 500 1200]);
Nindices_max = max(max_siz(1,2,:));
for mm = 1:Nmeasures
Nindices = max_siz(1,2,mm);
for nindex = 1:Nindices
subplot(Nindices_max,Nmeasures,(nindex-1)*Nmeasures+mm);
boxplot(permute(C{mm}(nindex,:,:),[2 3 1]));
title(sprintf('%s: %d',measures{mm},nindex))
end
end
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

