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

Fangjun Jiang
Fangjun Jiang 2011년 6월 28일
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 2011년 6월 28일
Yeah, that's what I meant. I meant to say that in the question. 1a goes with 2a. 1b with 2b, etc.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2011년 6월 28일

1 개 추천

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

Charles Dillon
Charles Dillon 2011년 6월 29일
Thanks for the help. Unfortunately, this doesn't seem to work. The following is my code:
function [output1, output2]=runbatch(smlfoldername, imlfoldername)
Folder1 = fullfile(tempdir, smlfoldername);
Folder2 = fullfile(tempdir, imlfoldername);
smlDir = dir(fullfile(Folder1, '*.sml'));
smlFile = {smlDir.name};
output1=zeros(length(smlFile));
output2=output1;
for iFile = 1:length(smlFile)
aFile = smlFile{iFile};
aName = fileparts(aFile);
[output1(iFile), output2(iFile)]=runfile(aFile, fullfile(Folder2, [aName, '.iml']));
end
end
(I assumed you'd meant the Folder1 in the last line of your code to be a Folder2, but it doesn't work either way anyway, so that isn't the issue). The code doesn't even run the loop. I rewrote it to output the smlDir and smlFile variables, and got the following:
smlDir =
0x1 struct array with fields:
name
date
bytes
isdir
datenum
smlFile =
{}
Jan
Jan 2011년 6월 29일
The "tempdir" is just the TEMP folder as an example. Folder1 should be smlfoldername and Folder2 should be imlfoldername.
"output1=zeros(length(smlFile))" is a bug, because it creates a square matrix, and not a vector. Use "output1=zeros(1, length(smlFile))" instead. For a pre-allocation create output2 by the ZEROS command also, which is slightly faster than a copy of another array.
Charles Dillon
Charles Dillon 2011년 6월 29일
Ok, thank you. So, to clarify, should I have
Folder1 = fullfile(smlfoldername);
Folder2 = fullfile(imlfoldername);
or
Folder1 = fullfile(smlfoldername, 'sml_files');
Folder2 = fullfile(imlfoldername, 'iml_files');
?
Or something else? If it is the second case, what are the purpose of the strings 'sml_files' and 'iml_files'?
Charles Dillon
Charles Dillon 2011년 6월 29일
Never mind, I got that part working, thank you!
Charles Dillon
Charles Dillon 2011년 6월 29일
It doesn't seem to be finding the files. I'm operating from a folder which contains all my functions and the two folders smlfiles and imlfiles which contain all the files needed, and yet when it runs the function within the loop, which contains usages of dlmread to read the contents of the files passed through by the runfile function, and these dlmreads cannot find the files passed through by the function (presumably because they are only looking in the folder from which I'm operating, rather than the subfolders which contain the files). How could I deal with this issue?
Jan
Jan 2011년 6월 29일
See the newly edited answer above: Use FULLFILE to create absolute file paths.
Charles Dillon
Charles Dillon 2011년 6월 29일
Thanks! That works perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stuart Layton
Stuart Layton 2011년 6월 28일

0 개 추천

Why can't you write your test function to take two inputs, one for each file?

댓글 수: 3

Charles Dillon
Charles Dillon 2011년 6월 28일
I don't get what you mean. The function can, and indeed, probably must take two inputs, that isn't a problem. I want to run every pair of files, where there are around 20-30 pairs of files. The example of a test function I gave above (runfile('1a.txt','2a.txt') does take two inputs. I just don't know how to get the files from the folders they are in.
Fangjun Jiang
Fangjun Jiang 2011년 6월 28일
help dir
Stuart Layton
Stuart Layton 2011년 6월 28일
yep, dir is what you want.

댓글을 달려면 로그인하십시오.

Fangjun Jiang
Fangjun Jiang 2011년 6월 28일

0 개 추천

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

Charles Dillon
Charles Dillon 2011년 6월 28일
The pairing rule is that they have the exact same names, with a different extension (one is .sml, the other .iml).
ie. One file will be called bg-38-20.sml (that is the name of one of the files) and its pair will be bg-38-20.iml.
Fangjun Jiang
Fangjun Jiang 2011년 6월 28일
See Jan Simon's solution above. You've got the idea, right?
Charles Dillon
Charles Dillon 2011년 6월 29일
I'm not really sure. The problem is that I don't really get from the MATLAB explanations what the outputs of the "dir" and "fullfile" functions are.
Jan
Jan 2011년 6월 29일
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 2011년 6월 29일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

질문:

2011년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by