combine multiple text files into one text file with a loop

11 ビュー (過去 30 日間)
Aaron Farkas
Aaron Farkas 2012 年 5 月 30 日
コメント済み: Walter Roberson 2019 年 2 月 23 日
Hi
I have multiple text files with names like 1.txt, 2.txt, 3.txt...... Each file has a single row of data(same format and length), I am think about combining those files into one text file which has a matrix data.
For example
1.txt has a row of " 1 3 4 8...."
2.txt has a row of " 3 5 7 3...."
3.txt has a row of " 9 1 3 5...."
......
I want to combine them into a file with a matrix like below,
1 3 4 8....
3 5 7 3....
9 1 3 5....
I know I can do system('copy 1.txt+2.txt+3.txt MyBigFat.txt'), but if I have over 100 or even 1000 text files can I save myself the trouble from doing system('copy 1.txt +2.txt+ 3.txt +...+1000.txt')?
Thanks in advance!
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 2 月 23 日
You can generate the command to system()
s = sprintf('%d.txt+', 1:999);
s = ['copy ', s, '+1000.txt MyBigFat.txt'];
system(s)

サインインしてコメントする。

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 5 月 30 日
k = arrayfun(@(x)dlmread([num2str(x),'.txt']),1:3,'un',0)
out = cat(1,k{:})
dlmwrite('4.txt',out,'delimiter', ' ')

Walter Roberson
Walter Roberson 2012 年 5 月 30 日
  2 件のコメント
Kjersti Ensrud
Kjersti Ensrud 2019 年 2 月 22 日
Do you know how to get each .txt-file to start at a new line. Something like \n ?
Walter Roberson
Walter Roberson 2019 年 2 月 23 日
Is it the case that you are reading in a number of files, and building up one large numeric array from them, and that you want to write out the numeric array, but with an empty line between where the boundaries of the files used to be?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by