MATLAB Answers

0

Values in cell array keep getting overwritten

Asked by zexi Leong on 13 Sep 2019
Latest activity Commented on by Stephen Cobeldick on 3 Dec 2019 at 15:26
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 326x1 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

  0 Comments

Sign in to comment.

2 Answers

Answer by Stephen Cobeldick on 16 Oct 2019
Edited by Stephen Cobeldick on 3 Dec 2019 at 15:25
 Accepted 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

  18 Comments

@zexi Leong: what was the issue? I notice you seem to have changed the filepath and/or OS
Remember to accept my answer if it helped you: accepting answers is an easy way to show your appreciation of the volunteers who help you on this forum.
Yes I switched computers hence the file path is different but the issue with the code was that for the exist function, I was supposed to be looking for a file and not a folder? So i changed it from 7 to 2. This resolved all of my problems.
"So i changed it from 7 to 2. This resolved all of my problems. "
Ah, well spotted! I will change my answer too, so that it does not confuse any future readers.

Sign in to comment.


Answer by Divya Yerraguntla on 16 Oct 2019

Hi Zexi
Try using the following modified code to serve your purpose. Have a look at the comments for a better understanding regarding the changes made.
parent_directory = dir;
num_dir = numel(parent_directory([parent_directory(:).isdir]))-2;% To find the number of sub folders on the parent directory
rt = cell(num_dir,1);
for i = 3:length(parent_directory) % The parent_directory.name field contains names of folders from the 3rd index location, hence i starts from 3
if ~isempty(strfind(parent_directory(i).name,'img'))
foldername = [parent_directory(i).folder '/' parent_directory(i).name];
cd(foldername);
if isempty(dir('stopchoicert.mat'))
continue;
end
load ('stopchoicert.mat');
rt{i-2,1}= a; % As i starts from 3, go back by 2 indices to fill rt
end
end
rt
Hope it helps!

  1 Comment

This is factually incorrect: "The parent_directory.name field contains names of folders from the 3rd index location, hence i starts from 3"
As anyone experienced with MATLAB (or anyone who reads this forum) will know, the order of the files returned by dir is determined by the OS, and not by MATLAB. None of the OS MATLAB runs on guarantee a particular order (although apparently this author assumed that there is one).
The author incorrectly assumed that the first two results correspond to the folder shortcuts '.' and '..', but in fact it is trivial to define some filenames where this is not the case:
>> fclose(fopen('+test.txt','wt'));
>> fclose(fopen('-test.txt','wt'));
>> fclose(fopen('@test.txt','wt'));
>> S = dir('*');
>> S.name
ans = +test.txt
ans = .test.txt
ans = .
ans = ..
ans = @test.txt
Bad code includes
  • the use of cd (slow, difficult to debug), rather than absolute/relative filenames.
  • string concatenation instead of fullfile.
  • loading directly into the workspace.

Sign in to comment.