How can I combine multiple text files with the same prefix but in different folders?
Show older comments
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
Jan
on 2 Aug 2013
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.
Nerys Astbury
on 10 Sep 2013
Edited: Walter Roberson
on 11 Sep 2013
Jan
on 10 Sep 2013
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?
dpb
on 10 Sep 2013
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.
Walter Roberson
on 11 Sep 2013
Should be
m = importdata(fullfile(dname, d.name));
and
m = [m; importdata(fullfile(dname, d.name))];
Answers (1)
dpb
on 2 Aug 2013
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
on 10 Sep 2013
Jan
on 10 Sep 2013
@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.
dpb
on 10 Sep 2013
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?
Nerys Astbury
on 11 Sep 2013
dpb
on 11 Sep 2013
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
Walter Roberson
on 11 Sep 2013
The suggested code wasn't prepending the directory name before importing the data.
dpb
on 11 Sep 2013
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.
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!