How to pull files from mutiple folders
Show older comments
I have a long list of subject folders (~70) I need to pull an individual summary file from each folder, scan and write the information to a new overall summary file.
I have most of the processing code written, however I am having issues automating the opening of each folder and extracting the data. I would prefer not to individually select each file.
Folder names are 'SubjectXX' and within each is a .txt file called 'Summary_SubjectXX.txt' along with other unnecessary folders.
I am using uigetdir to select the parent folder, but I cant seem to navigate into each folder after establishing the directory. Code I have so far is:
%%%
subjects_folder = uigetdir('*.txt');
cd(subjects_folder);
list_of_subject_folders = dir(subjects_folder);
for i = 3:numel(list_of_subject_folders)
file_to_process = dir('Subject[0-9][0-9]*'); %This line seems wrong to me
subjectID = regexp(file_to_process, 'Subject[0-9][0-9]*','match');
fid = sprintf('%s_%s', 'Summary', subjectID{1});
fID = fopen(fid, 'r');
scanned_data = textscan(fID, ['%*s', '\t', '%s', '\n']);
data_to_print = [subjectID, scanned_data{1, 1}(2:22)];
fclose(fID);
cd ../
end
%%%
I feel like I am just missing a simple step in identifying the folders, however I am relatively new to MATLAB and cannot seem to figure it out with just the 'help' function.
I appreciate any help or advice!
2 Comments
Walter Roberson
on 20 Jul 2012
MS Windows? I am not certain that [0-9] works for dir() in MS Windows. It should be okay in Linux or OS-X I would think.
Steven
on 20 Jul 2012
Accepted Answer
More Answers (3)
Walter Roberson
on 20 Jul 2012
Your line
fid = sprintf('%s_%s %s', 'Summary', subjectID{1});
expects three string inputs to sprintf(), but you are only passing in two.
Steven
on 20 Jul 2012
per isakson
on 20 Jul 2012
0 votes
See the FEX contributions:
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!