Plot data from nested structure

10 visualizzazioni (ultimi 30 giorni)
Susan
Susan il 11 Dic 2023
Commentato: Voss il 11 Dic 2023
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
Mathieu NOE
Mathieu NOE il 11 Dic 2023
maybe you should share some code and data
Susan
Susan il 11 Dic 2023
Modificato: Susan il 11 Dic 2023
Honestly, my code is such a mess, you all might run away in terror!
The structure with all data is probably too big to share, but I've attached a smaller version. I'm hoping I may have a cleaner solution than what I've been doing in something like this, but it's hanging up right now (probably due to missing values:
%Create a 3D matrix to plot features across conditions
boxplotmatrix = [];
for nsub = 1 : 17 %number of participants
for nactivity = 1 : 6 %stages of the trial for each participant
for nindex = 1 : 4 %metrics from the decomposed time series
%This next line is probably not going to work??? Some participants
%missing all data from EDA/ECG/EEG/etc, some just a single
%activity, but it's different for each participant & measure.
%These plots aren't for publication, just to view what we've
%got
if ~isempty(metrics.sub(nsub).EDA(nactivity, nindex))
end
boxplotmatrix(nindex,nsub,nactivity) = metrics.sub(nsub).EDA(nactivity, nindex);
end
end
end
Unable to resolve the name 'metrics.sub'.
%Create all boxplots with one for loop
for nindex = 1 : Nindices
boxplot(squeeze(boxplotmatrix(nindex,:,:))); % Note the command squeeze!
title(NameofIndex{nindex})
end

Accedi per commentare.

Risposta accettata

Voss
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
  2 Commenti
Susan
Susan il 11 Dic 2023
Wow, wow, wow! Thank you so much!!! I was up into the wee hours fighting with this last night & you make it look so easy!
Voss
Voss il 11 Dic 2023
You're welcome!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by