MATLAB: How to properly populate a cell array within a nested for loop (R2020a)

cell arraysfor looppopulating

I have a directory with four sub-directories. Each sub-directory contains files which I want to analyze, and there is an uneven number of files between sub-directories. I have created a for loop to go through each sub-directory and perform my analysis and record my results. To do this, I have two sub-loops.
The first sub-loop performs the analysis on each file within a sub-directory, and populates two empty arrays I pre-defined – "names" and "vRange".
The second sub-loop takes these two populated arrays and populates a third pre-defined empty array, "names_ranges", and is a cell array of size 14×12. I would like to populate this cell array with "names" and "vRange". I'm using a for loop because when I tried to insert the entire arrays all at once into the cell array, it returned an error "Expected one output from a curly brace or dot indexing expression, but there were 14 results". So, I'm now trying to populate the cell array one element at a time with a for loop. My end goal is to have "names_ranges" with "names" inserted at columns 1, 4, 7, 10 and to have "vRange" inserted at columns 2, 5, 8, 11. This is why I perform the operation "3*k-2" and "3*k-1" when indexing the cell-array.
The problem with my code is that the cell array I am looking for is not being generated. The only columns in "names_ranges" that are populated are columns 10 and 11. Column 10 is populated with the "name" arrays for only two sub-directories (I have 4), while column 11 is populated with the "vRange" arrays for only two-subdirectories as well. No errors are being generated. Below are my attempts as well as my entire code for context.
The following code is my initial attempt at populating the cell array by inserting the entire arrays:
% Populate empty cell array with vRange and names

names_ranges{:,(3*k-2)} = names(:);
names_ranges{:,(3*k-1)} = vRange(:);
This code is my current attempt at using a for loop to populate the cell array by inserting each element of the arrays one at a time:
% Populate empty cell array with vRange and names
for p = 1:length(names)
names_ranges{p,(3*k-2)} = names(p);
names_ranges{p,(3*k-1)} = vRange(p);
end
This is my entire code, for context:
% folder_list contains everything in the parent folder
parent = '/Users/Ritchie/Desktop/Data/Mouse 1';
folder_list = dir(parent_folder);
% Remove non-directoriers
directoryNames = {folder_list([folder_list.isdir]).name};
% Remove '.' and '..' using ismember
directoryNames = directoryNames(~ismember(directoryNames,{'.','..'}));
directoryNames = char(directoryNames);
full_table = cell([14,12]);
% Loop through sub-directories
for k = 1 : length(directoryNames)
s = what(directoryNames(k))
myFolder = s.path;
filePattern = fullfile(myFolder, '*.dam');
theFiles = dir(filePattern);
% Create three empty arrays
vRange = zeros(length(theFiles),1);
names = strings(length(theFiles),1);
names_ranges = cell(14,12);
% Perform analysis on the files within a sub-directory
for i = 1 : length(theFiles)
baseFileName = theFiles(i).name;
fullFileName = fullfile(directoryNames(k), baseFileName);
xx = damFileRead(fullFileName);
samples = 1:24415;
Fs = 24.4140625;
t = samples/Fs;
yy = (mean(horzcat(xx.signal),2));
z = range(yy)/3276.5;
% Populate empty arrays with results
vRange(i) = z;
names(i) = baseFileName;
end
% Populate empty cell array with names and vRange
for p = 1:length(names)
names_ranges{p,(3*k-2)} = names(p);
names_ranges{p,(3*k-1)} = vRange(p);
end
end

Best Answer

Just kidding, I solved the problem. If it's of any help to anyone in the future with a similar problem and wants to populate a cell array, below is the code which worked for me. The reason why my code in the question above wasn't working was because I was making the empty cell array "names_ranges" inside the for loop. This meant that every loop was erasing the progress from before, and explains why only columns 10 and 11 were populated. I also simplified the code by removing the two arrays "names" and "vRange" and directly populating the cell array with a single sub-loop.
% folder_list contains everything in the parent folder
parent = '/Users/Ritchie/Desktop/Data/Mouse 1';
folder_list = dir(parent);
% Remove non-directoriers so that directoryNames only contains directories
directoryNames = {folder_list([folder_list.isdir]).name};
% Remove '.' and '..' using ismember
directoryNames = directoryNames(~ismember(directoryNames,{'.','..'}));
directoryNames = char(directoryNames);
% Create empty array
names_ranges = cell(14,12);
% Loop through directories
for k = 1 : length(directoryNames)
s = what(directoryNames(k));
myFolder = s.path;
filePattern = fullfile(myFolder, '*.dam');
theFiles = dir(filePattern);
% Perform analysis on the files within each directory
for i = 1 : length(theFiles)
baseFileName = theFiles(i).name;
fullFileName = fullfile(directoryNames(k), baseFileName);
xx = damFileRead(fullFileName);
samples = 1:24415;
Fs = 24.4140625;
t = samples/Fs;
yy = (mean(horzcat(xx.signal),2)); % "2" is needed to average across columns and not rows, which would be "1"
z = range(yy)/3276.5;
% Populate empty cell array <-- this is the important step
names_ranges{i,3*k-2} = baseFileName; % 3*k-2 references the column that's being populated for each main loop
names_ranges{i,3*k-1} = z;
end
end