MATLAB Answers

How to cycle through two folders and search for files with similar names and load them into current directory

1 view (last 30 days)
Natalie Cannon
Natalie Cannon on 19 Aug 2020
Answered: Geoff Hayes on 19 Aug 2020
I have two folders (RAW_Data & SCALED_Data) each with 65 .Txt data files. The data files are named using the following format: 'InM##_RAW' in the RAW folder or 'FnM##_SCALED' in the Scaled Folder (## starting from 01 and going through 65). What I'm trying to do is create a loop that will automatically cycle through both folders, find matching pairs and copy them into my current directory.
I have been using 'copyfile', however I have to manually specify which number I'm looking for each time & don't want to do that anymore.
Thanks for any help or guidance

  0 Comments

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 19 Aug 2020
Natalie - a loop could look like the following
for k=1:65
rawFilename = sprintf('InM%02d_RAW.txt', k);
scaledFilename = sprintf('FnM%02d_SCALED.txt', k);
% find matching pairs
% copy the files
end
I'm assuming you have the folder names which you can prepend to the above using fullfile and that you are doing some sort of check like with isfile to see if the two files exist before moving them into the target folder.

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!