Plot multiple cases from data structure at once?

1 次查看(过去 30 天)
Hi. I have a data structure with multiple files and multiple participants. I'd like to plot all participants at once and run statistical analyses on the data. Structure setup and sample plot are below. I'm not receiving specific errors, I'm just new to using data structures and just cant figure out how to call all participants at once.
%Structure setup
for i = 1:length(parts)
data.EDA = readtable(join([mypath,'/',parts(i),'/EDA.csv'],''));
data.ECG = readtable(join([mypath,'/',parts(i),'/ECG.csv'],''));
if i ~= 2
data.EEG = readtable(join([mypath,'/',parts(i),'/EEG.csv'],''));
end
data.PPG = readtable(join([mypath,'/',parts(i),'/PPG.csv'],''));
data.ACC = readtable(join([mypath,'/',parts(i),'/ACC.csv'],''));
data.BR = readtable(join([mypath,'/', parts(i),'/DataAverage.csv'],''));
disp('Participant Structure.');
name = ['Subject_Structure',num2str(i), '.mat']; %name the structure
save(fullfile(join([mypath,'/',parts(i)],''),name),'data'); %save in the path with the correct data
end
Unrecognized function or variable 'parts'.
%%
%%
times = table2array(info(1:17,2:7)); %START TIMES ONLY - END TIMES MISSING
subN=6;
load(join([parts(subN), '/Subject_Structure', num2str(subN),'.mat'],''));
%% %% %% ---- EDA ----------------------------------------------- %%
%Pull the data from the structure for EDA
EDA = table2array(data.EDA);
fs=4;
% approx 20 lines of signal procesing code goes here
figure;
tiledlayout(6, 2);
nexttile([3 1]);
plot(time_EDA, EDA);
xlim([0, EDA_tics0(6) + 330]);
xline(EDA_tics0(1), '--', 'color', '#2980B9', 'LineWidth', 1);
xline(EDA_tics0(2), '--', 'color', '#5DADE2', 'LineWidth', 1);
xline(EDA_tics0(3), '--', 'color', '#1ABC9C', 'LineWidth', 1);
xline(EDA_tics0(4), '--', 'color', '#82E0AA', 'LineWidth', 1);
xline(EDA_tics0(5), '--', 'color', '#F8C471', 'LineWidth', 1);
xline(EDA_tics0(6), '--', 'color', '#E74C3C', 'LineWidth', 1);
xline(EDA_tics0(6) + 300, '-k', 'LineWidth', 2);
xlabel('time (s)');
ylabel('EDA (\muS)');
title('EDA with Onsets, Participant 11 (IFIS = "Poor")');
legend('EDA', 'Sit', 'Stand', 'Bike Low', 'Bike High', 'Run Low', 'Run High', 'Expected End', 'Location','northwest','NumColumns',2);
  3 个评论
Susan
Susan 2023-12-4
Oh no. This file has about 1200 lines of code as of now. I'm just trying to get advice for a generic way to plot the data (ie: data.EDA) for all participants at once, in a single, tiled layout (I can handle the layout).
I'm relatively new to MATLAB and this is my first project using a data structure. I can't find anywhere in the documentation how to call all cases for a single variable at once. So, where I'm calling the 16th participant, in the bolow code chunk, I'd like to have it run a plot for all cases at the same time, so I can visually inspect them.
subN = 6; %Identify the participant # to plot
%Instead of the following line that calls a single participant, I'd like to load ALL cases at once
load(join([parts(subN), '/Subject_Structure', num2str(subN),'.mat'],'')); %Extract the data for the identified participant
EDA = table2array(data.EDA); %Extract the variable to be plotted
tiledlayout('flow') %Here I would like to plot all 16 participants' data into a single figure
plot(time_EDA, EDA);
xlim([0, EDA_tics0(6) + 330]);
Voss
Voss 2023-12-4
%Structure setup
for i = 1:length(parts)
data.EDA = readtable(join([mypath,'/',parts(i),'/EDA.csv'],''));
data.ECG = readtable(join([mypath,'/',parts(i),'/ECG.csv'],''));
if i ~= 2
data.EEG = readtable(join([mypath,'/',parts(i),'/EEG.csv'],''));
end
data.PPG = readtable(join([mypath,'/',parts(i),'/PPG.csv'],''));
data.ACC = readtable(join([mypath,'/',parts(i),'/ACC.csv'],''));
data.BR = readtable(join([mypath,'/', parts(i),'/DataAverage.csv'],''));
disp('Participant Structure.');
name = ['Subject_Structure',num2str(i), '.mat']; %name the structure
save(fullfile(join([mypath,'/',parts(i)],''),name),'data'); %save in the path with the correct data
end
Note that when i is 2, data still has the EEG field left over from when i was 1 (because data is not cleared or re-initialized on each iteration of the loop). So the mat-file corresponding to i==2 will contain the EEG data for participant 1. I suspect this is not what's intended.

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2023-12-4
For each participant, load the participant's mat file and plot the EDA data into the next tile of a tiledlayout.
% number of participants:
N = numel(parts);
% create a tiledlayout for plotting:
figure;
tiledlayout(ceil(N/2), 2);
% loop over participants:
for ii = 1:N
% new tile to plot to:
nexttile();
% load and plot EDA data for participant ii:
S = load(fullfile(mypath,parts(ii),sprintf('Subject_Structure%d.mat',ii)));
plot(time_EDA, table2array(S.data.EDA));
% xlim:
xlim([0, EDA_tics0(6) + 330]);
% xlines:
xline(EDA_tics0(1), '--', 'color', '#2980B9', 'LineWidth', 1);
xline(EDA_tics0(2), '--', 'color', '#5DADE2', 'LineWidth', 1);
xline(EDA_tics0(3), '--', 'color', '#1ABC9C', 'LineWidth', 1);
xline(EDA_tics0(4), '--', 'color', '#82E0AA', 'LineWidth', 1);
xline(EDA_tics0(5), '--', 'color', '#F8C471', 'LineWidth', 1);
xline(EDA_tics0(6), '--', 'color', '#E74C3C', 'LineWidth', 1);
xline(EDA_tics0(6) + 300, '-k', 'LineWidth', 2);
% etc.:
xlabel('time (s)');
ylabel('EDA (\muS)');
title(sprintf('EDA with Onsets, Participant %d (IFIS = "Poor")',ii)); % titles change by participant
legend('EDA', 'Sit', 'Stand', 'Bike Low', 'Bike High', 'Run Low', 'Run High', 'Expected End', 'Location','northwest','NumColumns',2);
end
  1 个评论
Voss
Voss 2023-12-4
By the way, here is a way to store the data from all files for all participants in a struct array S, rather than multiple mat files:
% field names and corresponding file/data types:
fields = {'EDA', 'ECG', 'EEG', 'PPG', 'ACC', 'BR'};
files = {'EDA.csv','ECG.csv','EEG.csv','PPG.csv','ACC.csv','DataAverage.csv'};
% construct full-path file names for all participants and file-types (EDA/ECG/etc.):
F = string(mypath) + filesep() + string(parts(:).') + filesep() + string(files(:));
% M is number of file-types; N is number of participants:
[M,N] = size(F);
% initialize a 1-by-N struct array with fields from 'fields'
% and empty data in each field:
S = [];
for jj = 1:M
S(N).(fields{jj}) = [];
end
% fill in the struct array with data from the files:
for ii = 1:N % loop over participants
for jj = 1:M % loop over file-types
if ii == 2 && jj == 3 % no EEG data for participant #2, apparently
continue
end
% readtable() file F(jj,ii), and store the resulting table in the
% jj-th field of the ii-th element of S:
S(ii).(fields{jj}) = readtable(F(jj,ii));
end
end
Then you can loop over each element of that struct array and plot the EDA data (and/or whatever else):
% create a tiledlayout for plotting:
figure;
tiledlayout(ceil(N/2), 2);
% loop over participants:
for ii = 1:N
% new tile to plot to:
nexttile();
% plot EDA data for participant ii:
plot(time_EDA, table2array(S(ii).EDA));
% xlim:
xlim([0, EDA_tics0(6) + 330]);
% xlines:
xline(EDA_tics0(1), '--', 'color', '#2980B9', 'LineWidth', 1);
xline(EDA_tics0(2), '--', 'color', '#5DADE2', 'LineWidth', 1);
xline(EDA_tics0(3), '--', 'color', '#1ABC9C', 'LineWidth', 1);
xline(EDA_tics0(4), '--', 'color', '#82E0AA', 'LineWidth', 1);
xline(EDA_tics0(5), '--', 'color', '#F8C471', 'LineWidth', 1);
xline(EDA_tics0(6), '--', 'color', '#E74C3C', 'LineWidth', 1);
xline(EDA_tics0(6) + 300, '-k', 'LineWidth', 2);
% etc.:
xlabel('time (s)');
ylabel('EDA (\muS)');
title(sprintf('EDA with Onsets, Participant %d (IFIS = "Poor")',ii)); % titles change by participant
legend('EDA', 'Sit', 'Stand', 'Bike Low', 'Bike High', 'Run Low', 'Run High', 'Expected End', 'Location','northwest','NumColumns',2);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Frequently-used Algorithms 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by