Values in cell array keep getting overwritten
조회 수: 9 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2019년 10월 16일
편집: Stephen23
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
Stephen23
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.
추가 답변 (1개)
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
Stephen23
2019년 10월 16일
편집: Stephen23
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!