MATLAB: Values in cell array keep getting overwritten

cell arraysfor loop

Hi there, I am new at MATLAB and currently trying to extract data from a particular subfolder and put it into a new cell array
basically i want to retrieve mean reaction times which is stored in a variable called a (location: parent_directory > containing many subject folders labelled img0001, img0002 etc > each subject folder contains a .mat file called stopchoicert.mat > variable a can be found here)
My code is as follows but instead of every iteration being placed into cell array, the values keep getting overwritten so i only have the mean reaction time of my last subject.
I have around 325 subjects but not all of them have mean reaction times, so I am unsure how many values in my cell array I should be getting at the end but the output rt that i get is a 326×1 cell array although only the 80th row contains the mean reaction time (of the last subject) and the rest of the cells are a 0X0 double
parent_directory = dir; %setting up the parent file to access files
rt = cell(size(parent_directory)); %an empty cell to insert all reaction times
for i = 1:length(parent_directory)
if ~isempty(strfind(parent_directory(i).name,'img')) %find folders with img in it
foldername = [parent_directory(i).folder '/' parent_directory(i).name]; %create a new path
cd(foldername); %loop through the paths
if isempty(dir('stopchoicert.mat'))
continue; %if folder does not contain stopchoicert.mat then skip
end
load ('stopchoicert.mat')
rt{i,1}= a %a contains the mean reaction time which i want to store in cell array
end %loop through all folders to extract all mean reaction times and store into cell array
end
rt
please help and lmk if you need any more info

Best Answer

Much simpler and much more robust:
D = 'path to the main directory';
S = dir(fullfile(D,'img*'));
for k = 1:numel(S)
F = fullfile(D,S(k).name,'stopchoicert.mat');
T = load(F);
S(k).data = T.a;
end
C = {S.data};
If not all of the subfolders contain the file, then you could do something like this:
for k = 1:numel(S)
F = fullfile(D,S(k).name,'stopchoicert.mat');
if exist(F,'file')==2
T = load(F);
S(k).data = T.a;
end
end