MATLAB: Vertically concatenate blocks of arrays

blocks of vertcatfor loopMATLABvertcat

Hi everyone,
I want to concatenate the arrays from 4 files, (each corresponding to a block in a task) for every subject in a list. Each file has 5 variables of interest with 60 rows. I combine them to create a 60×5 array for every block.
I want to concatenate the 4 arrays, to create a single 240×5 array. I repeat this for another phase, where I am trying to generate an output of the same dimensions.
Then, I would like to store these arrays inside cells, to create a Nx3 cell array (N= subjects) with the first column being the ID, then the first phase 240×5 array and then the second phase 240×5 array.
The problem I have right now is that my loop keeps parsing arrays in every cell, so in {2,2} I have a 480×5, in {3,2} a 720×5 and so on.
I know that probably it has something to do with the general organization of my loops, but I am struggling finding the right arrange. Could you please guide me on how to fix this issue? I hope my explanation is clear, otherwise let me know.
Best, Ramiro
%%Empty matrices to populate with results
matLenght = lastID-20;
respMat = cell(matLenght,3);
lrnMatrix = zeros(0,5);
expMatrix = zeros(0,5);
%%Loop through each file to get weights
for k = firstID:lastID
adjustedPosition = k-20;
subject = num2str(k);
for kk = 1:4
block = num2str(kk);
lrnFile = [subject '_LP_Block' block '.mat'];
fileDir = fullfile(dataDir, lrnFile);
load (fileDir)
trials = 60;
location = trials*block;
lrnData = horzcat(dE.leftWeight, dE.rightWeight, dE.timeTrial, dE.keyResp, dE.bestDecision);
lrnMatrix = vertcat(lrnMatrix, lrnData);
end
for kk = 1:4
block = num2str(kk);
expFile = [subject '_EP_Block' block '.mat'];
fileDir = fullfile(dataDir, expFile);
load (fileDir)
trials = 60;
location = trials*block;
expData = horzcat(dE.leftWeight, dE.rightWeight, dE.timeTrial, dE.keyResp, dE.bestDecision);
expMatrix = vertcat(expMatrix, expData);
end
%%Insert subject ID in the first column of the array
respMat{adjustedPosition,1} = k;
respMat{adjustedPosition,2} = lrnMatrix;
respMat{adjustedPosition,3} = expMatrix;
end

Best Answer

The problem I have when reading your code, other than the lack of comment, is that I come across
load(fileDir)
At this point, I have no way of knowing what variables exist. A load unassigned to any variable pops unknown variables into existence, potentially overwriting existing ones without any warning. For all we know, it's replaced all the variables you've just created by new ones.
I'm guessing that the load creates at least one structure called dE, otherwise your code makes no sense. Personally, I would use:
matcontent = load(fileDir);
dE = matcontent.dE;
expData = [dE.leftWeight, dE.rightWeight, dE.timeTrial, dE.keyResp, dE.bestDecision];
Now, if I understood correctly, the problem you have is that you're not resetting your lrnmatrix and expMatrix to empty in between subjects. That's easily fixed with by moving their allocation inside the k loop:
for subject = (firstID:lastID)-20 %your k loop
lrnMatrix = zeros(0,5);
expMatrix = zeros(0,5);
for block = 1:4
lrnFile = sprintf('%d_LP_Block%d.mat', subject, block);
lrncontent = load(fullfile(dataDir, lrnFile));
dE = lrncontent.dE;
expData = [dE.leftWeight, dE.rightWeight, dE.timeTrial, dE.keyResp, dE.bestDecision];
lrnMatrix = [lrnMatrix; expData]; %#ok<AGROW> suppress warning about growing a matrix in loop

expFile = sprintf('%d_EP_Block%d.mat', subject, block);
expcontent = load(fullfile(dataDir, expFile));
dE = expcontent.dE;
expData = [dE.leftWeight, dE.rightWeight, dE.timeTrial, dE.keyResp, dE.bestDecision];
expMatrix = [expMatrix; expData]; %#ok<AGROW> suppress warning about growing a matrix in loop
end
respMat{subject, 1} = subject+20;
respMat{subject, 2} = lrnMatrix;
respMat{subject, 3} = expMatrix;
end
I've made some other changes which I think make the code cleaner (including removing the calculation of location since it wasn't used).