How can I combine multiple text files with the same prefix but in different folders?

I'm looking to concatenate multiple .txt files saved with the same prefix (mp) saved in separate folders (1-10) as one large txt or .mat file which I can then use in other MATLAB scripts. There are ten .txt files each stored in a separate folder numbered 1-10. All have long names but all start with 'mp'
Please help

5 Comments

What is your question? What have you tried so far? The topic of importing files in a loop is discussed frequently in this forum such that I can recommend to search in the forum at first.
I've searched the forums- but I can't find an answer that works! I've ended up tying the whole command out manually- since I still can't get a loop to work:
Basically what I have is:
cd 1
file = dir('rp*')
m1 = load(file(1).name)
cd ..
Which I then repeat for folders 2-10 then:
combined= [m1; m2; m3; m4; m5; m6; m7; m8; m9; m10]
Why do you use load to read txt files? Why do you search for "rp*", when the files start with 'mp'? What is the problem with your code? How could the list of folders be obtained?
Note that concatenating 10 text files in an editor can be done manually in less than a minute. Is it really worth to write a Matlab program for this?
cd 1
file = dir('rp*');
m1 = load(file(1).name)
cd ..
Yet again, use COPY
But, to do it this way (hard way 'round, yet again, though)...
Assume in higher level directory to begin with...
dname='1'; % First directory in the ten...
d=dir(fullfile(dname,'mp*.txt')); % get the file in that directory
m=importdata(d.name);
for i=2:10
dname=num2str(i); % Next directory in sequence
d=dir(fullfile(dname,'mp*.txt')); % get the file in that directory
m=[m;importdata(d.name)];
end
% do whatever w/ the result here...
The above has the potential big performance problem if the files are large of dynamic allocation/reallocation in the loop that the COPY solution doesn't suffer from.
Should be
m = importdata(fullfile(dname, d.name));
and
m = [m; importdata(fullfile(dname, d.name))];

Sign in to comment.

Answers (1)

I'd use the shell COPY command either just directly from the shell or if really must needs be programmatically sotoo...
dname='DIR1'; % First directory in the ten...
d=dir(fullfile(dname,'mp*.txt')); % get the file in that directory
cmd=['copy ' d.name ' ' 'bigfile.txt'];
!cmd % copy the first to a target
for i=2:10
dname=sprintf('DIR%d',i); % Next directory in sequence
d=dir(fullfile(dname,'mp*.txt')); % get the file in that directory
cmd=['copy bigfile.txt + ' d.name]; % and append to bigfile.txt
!cmd
end
Warning!!! Aircode; salt to suit... :)

7 Comments

@Nerys Astbury: You offer only vague information about your problem and your trials to solve it. Answering requires too much guessing to be efficient. Please post more detailed information.
For what definition of "doesn't work?"
Sorry, the crystal ball is in the shop again yet today.
But again, I'd do something like this at the command line in a batch file or in Explorer instead of Matlab -- or, I'd create the files in a more suitable format to begin with--at least put them in the same working directory. Why make more trouble for no purpose?
I have 200+ folders each containing two sub-folders A&B, in each A and B are the folders 1-10 (as above), which each contain a number of files- but one file in particular a .txt file which is called mp*
I am working in Matlab- hence why I would like a script to concatenate all 10 mp*.txt into one (can be a mat file) for use in the next phase of my Matlab program. So concatenating manually 4000 files is not an option!
I'm currently going into each folder: 1-200\A and 1-200\B and doing this:
cd 1
file = dir('mp*')
m1 = load(file(1).name)
cd ..
for each of the folders 1-10, then..
combinedmp = [m1; m2; m3; m4; m5; m6]
Surely there is a better way?
I've given you code three times now -- two what I'd suggest is more efficient to simply copy the files together in the OS w/ COPY and another to actually load and concatentate.
You've complained it "didn't work" but haven't provided any detail on what, specifically, was the problem. Sorry, nothing more I know to do w/o specific code and the result that wasn't satisfactory to look at--the crystal ball is cloudy.
Sorry, can't help further
The suggested code wasn't prepending the directory name before importing the data.
Ah...good catch; I warned OP was "air code" ... :)
At the keyboard didn't think about that despite the explicit name in the dir argument that .name in the returned structure is only the file, not the fully qualified name.
One would have thought it wouldn't have taken much effort for OP to recognize the issue, though, and correct same.

Sign in to comment.

Categories

Products

Asked:

on 2 Aug 2013

Community Treasure Hunt

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

Start Hunting!