MATLAB Answers

Values in cell array keep getting overwritten

11 ビュー (過去 30 日間)
zexi Leong
zexi Leong 2019 年 9 月 13 日
Commented: Stephen Cobeldick 2019 年 12 月 3 日
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 件のコメント

サインイン to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 10 月 16 日
編集済み: Stephen Cobeldick 2019 年 12 月 3 日
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 件のコメント

表示 15 件の古いコメント
Stephen Cobeldick
Stephen Cobeldick 2019 年 12 月 3 日
@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.
zexi Leong
zexi Leong 2019 年 12 月 3 日
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.
Stephen Cobeldick
Stephen Cobeldick 2019 年 12 月 3 日
"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.

サインイン to comment.

More Answers (1)

Divya Yerraguntla
Divya Yerraguntla 2019 年 10 月 16 日
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 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 10 月 16 日
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.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by