How to run a batch of pairs of files?

3 views (last 30 days)
I have what I assume is a dumb question, yet I cannot find an answer to it. I want to write a programme to run a bunch of tests through a function and store the answers produced. Each run of the function requires two files, and each file has a pair with which it must be run. Something like: runfile('1a.txt', '2a.txt'); where 1a.txt and 2a.txt are a pair of files. How could I get a program to run each pair of files? I assume I should first separate the two types of files into separate folders. Once I do that, and they are in the same order in each folder (ie. file 1a is the first file in folder 1, and file 2a is the first in folder 2 etc.), what should I use to get the files from the folders and run them through the function?
  2 Comments
Fangjun Jiang
Fangjun Jiang on 28 Jun 2011
What makes the two files to be a pair? As long as you can specify the rules (maybe by naming like '1a' paired with'2a', '1b' paired with '2b'?), there should be a way to pair them.
Charles Dillon
Charles Dillon on 28 Jun 2011
Yeah, that's what I meant. I meant to say that in the question. 1a goes with 2a. 1b with 2b, etc.

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Jun 2011
Files do not have an order in folders. Although DIR replies the files in alphabetically sorted order, this is not guaranteed.
[EDITED] You have explained the rule. Then: [EDITED 2] Considering your comments:
function runbatch(sml_folder, iml_folder)
smlDir = dir(fullfile(sml_folder, '*.sml'));
smlFile = {smlDir.name};
for iFile = 1:length(smlFile)
aFile = smlFile{iFile};
aName = fileparts(aFile);
aSMLFile = fullfile(sml_folder, aFile);
aIMLFile = fullfile(iml_folder, [aName, '.iml']);
runfile(aSMLFile, aIMLFile);
end
  7 Comments
Jan
Jan on 29 Jun 2011
See the newly edited answer above: Use FULLFILE to create absolute file paths.
Charles Dillon
Charles Dillon on 29 Jun 2011
Thanks! That works perfectly.

Sign in to comment.

More Answers (2)

Stuart Layton
Stuart Layton on 28 Jun 2011
Why can't you write your test function to take two inputs, one for each file?
  3 Comments
Stuart Layton
Stuart Layton on 28 Jun 2011
yep, dir is what you want.

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 28 Jun 2011
So, what is the pairing rule? Why don't you show us the complete file names in one set and the pairing file names in another set? The following example shows how it can be done if the rules are as simple as you explained so far.
FileOneSet={'1a.txt','1b.txt','1c.txt'};
FileTwoSet=strrep(FileOneSet,'1','2');
for k=1:3
runfile(FileOneSet{k},FileTwoSet{k});
end
  5 Comments
Jan
Jan on 29 Jun 2011
DIR creates a list of files from a directory, similar to the DIR command in Windows or ls in Unix, but with a struct vector as output.
FULLFILE concatenates parts of a file path considering initial and final file separators:
fullfile('C:\Temp', 'SubFolder'), fullfile('C:\Temp\', 'SubFolder'), fullfile('C:\Temp\', '\SubFolder'), fullfile('C:\Temp', '\SubFolder') reply the same string 'C:\Temp\SubFolder', such that the programmer do not have to care about the separators.
Fangjun Jiang
Fangjun Jiang on 29 Jun 2011
If you don't understand a particular function, you can type "help dir" or "doc dir" for example to find its usage and syntax. You can also put a break point in your .m file and run step by step to understand each line of code better.

Sign in to comment.

Categories

Find more on File Operations 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!