creating subplots for different array values

16 次查看(过去 30 天)
Asking for patience as I am very new to coding and only started Matlab in October. Not sure if my code if correct but it is running and looks like it's doing what I hope.
I have data that has been run through a DNN so I have 25 layers for each participant, each in a separate file which makes 100 files for 4 participants for example.
I would like to pull out the data from only 3 specific layers for only 4 specific participants and plot that data on 3 separate subplots by layer. I currently have VERY long code that I believe does the job, but I feel like nested for loops may be possible. Among other things, I can't figure out how to do that with the different subplots. For the moment, as you can see, I have manually filled in the layer (ll) and subplot position.
layerSel = [1 13 25]; % Selected layers
indLaySel = layerSel([1:end])
ssSel = [102 104 112 121]; % Selected participants
selSS = ssSel([1:end])
nBlocks = 10;
nTrialsPerBlock = 51;
stimLang = strcmp(batch_types,'valid')'; % creates column vector with value of 1 when 'valid' in batchtypes
stimLang = stimLang(1:nBlocks*nTrialsPerBlock).';
for ll = 1
for ss = selSS
load(['proj24/english_layer' num2str(layerSel(ll)) '_ss' num2str(ss) '_tr.mat'])
%creates stimulus matrix
stimulus = stimulus_durations(1:10,:); % takes rows 1-10, all columns
stimLengthV = stimulus.'; % pivots data vertically
stimLength = stimLengthV(:); % takes all columns and puts in 1 column, codes as x variable
stimLengthR = round(stimLength);
%creates distance matrix
avDist = avDists(:,:,3); % takes third dimension from avDist
avDist = avDist(:,:,1); % takes all rows, all columns, sheet 1
distance = avDist(:); % takes all columns from sheet 1 and puts in 1 column
% create table for ease of viewing ;)
dataTable = table(stimLength,distance,stimLang);
%create plot
subplot(1,3,1);
gscatter(stimLengthR,distance,stimLang,"kb","ox")
title('Layer 1')
xlabel('stimulus length')
ylabel('distance')
legend('pseudo','valid')
end
end
for ll = 13
for ss = selSS
load(['/proj24/english_layer' num2str(layerSel(ll)) '_ss' num2str(ss) '_tr.mat'])
%creates stimulus matrix
stimulus = stimulus_durations(1:10,:); % takes rows 1-10, all columns
stimLengthV = stimulus.'; % pivots data vertically
stimLength = stimLengthV(:); % takes all columns and puts in 1 column, codes as x variable
stimLengthR = round(stimLength);
%creates distance matrix
avDist = avDists(:,:,3); % takes third dimension from avDist
avDist = avDist(:,:,1); % takes all rows, all columns, sheet 1
distance = avDist(:); % takes all columns from sheet 1 and puts in 1 column
% create table for ease of viewing ;)
dataTable = table(stimLength,distance,stimLang);
%create plot
subplot(1,3,2);
gscatter(stimLengthR,distance,stimLang,"kb","ox")
title('Layer 13')
xlabel('stimulus length')
ylabel('distance')
legend('pseudo','valid')
end
end
for ll = 25
for ss = selSS
load(['/proj24/english_layer' num2str(layerSel(ll)) '_ss' num2str(ss) '_tr.mat'])
%creates stimulus matrix
stimulus = stimulus_durations(1:10,:); % takes rows 1-10, all columns
stimLengthV = stimulus.'; % pivots data vertically
stimLength = stimLengthV(:); % takes all columns and puts in 1 column, codes as x variable
stimLengthR = round(stimLength);
%creates distance matrix
avDist = avDists(:,:,3); % takes third dimension from avDist
avDist = avDist(:,:,1); % takes all rows, all columns, sheet 1
distance = avDist(:); % takes all columns from sheet 1 and puts in 1 column
% create table for ease of viewing ;)
dataTable = table(stimLength,distance,stimLang);
%create plot
subplot(1,3,3);
gscatter(stimLengthR,distance,stimLang,"kb","ox")
title('Layer 25')
xlabel('stimulus length')
ylabel('distance')
legend('pseudo','valid')
end
end

采纳的回答

Voss
Voss 2024-1-28
layerSel = [1 13 25]; % Selected layers
ssSel = [102 104 112 121]; % Selected participants
nBlocks = 10;
nTrialsPerBlock = 51;
stimLang = strcmp(batch_types,'valid')'; % creates column vector with value of 1 when 'valid' in batchtypes
stimLang = stimLang(1:nBlocks*nTrialsPerBlock).';
Nlayers = numel(layerSel);
Nparticipants = numel(ssSel);
figure();
for ll = 1:Nlayers
for ss = 1:Nparticipants
S = load(fullfile('proj24',sprintf('english_layer%d_ss%d_tr.mat',layerSel(ll),ssSel(ss))));
%creates stimulus matrix
stimLengthR = round(reshape(S.stimulus_durations(1:10,:).',[],1));
%creates distance matrix
distance = reshape(S.avDists(:,:,3),[],1);
%create plot
subplot(1,Nlayers,ll);
gscatter(stimLengthR,distance,stimLang,"kb","ox")
hold on
end
title(sprintf('Layer %d',layerSel(ll)));
xlabel('stimulus length')
ylabel('distance')
legend('pseudo','valid')
end
  6 个评论
Paquerette
Paquerette 2024-1-31
I also just reworked so I could use the scatter() function instead of gscatter() - very late to the game in coding but finding it quite fun

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by