フィルターのクリア

combine multiple text files into one text file

120 ビュー (過去 30 日間)
Cody
Cody 2011 年 7 月 5 日
コメント済み: michael fried 2022 年 3 月 22 日
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....
...........
...........
Thanks a lot
  1 件のコメント
ranjith J
ranjith J 2018 年 10 月 29 日
source code need

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

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 5 日
In DOS, I used to use this all the time.
copy 1.txt+2.txt+3.txt MyBigFat.txt
In MATLAB, you can use
system('copy 1.txt+2.txt+3.txt MyBigFat.txt')
  5 件のコメント
Robert
Robert 2018 年 3 月 29 日
This gives me a SUB character at the end of each appended file (https://en.wikipedia.org/wiki/Substitute_character). Do you know how to prevent this, or how to remove these? Regexping doesn't work as the files are too large.
michael fried
michael fried 2022 年 3 月 22 日
Use parameter "b" (binary) in order to avoid that character...:
In Windows Command Line:
copy /b 1.txt+2.txt+3.txt MyBigFat.txt
Or in Matlab:
system('copy /b 1.txt+2.txt+3.txt MyBigFat.txt')

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

その他の回答 (3 件)

Jason Ross
Jason Ross 2011 年 7 月 5 日
Keep in mind you don't even need MATLAB to do this. You can do it with built in OS commands on Windows or UNIX
Windows:
type 1.txt > bigfile.txt
type 2.txt >> bigfile.txt
UNIX:
touch bigfile.txt
cat 1.txt >> bigfile.txt
cat 2.txt >> bigfile.txt
This will result in one big file that's in the format you want. Of course, there's no error checking, but the big upside is going to be that there is very little overhead so your processing may go considerably faster depending on the number of files you have to do.
Depending on the order of the file names, this may also be very easily scriptable. You will, of course need to use one of the import functions in MATLAB to read it in eventually, as well.

Nirmal Gunaseelan
Nirmal Gunaseelan 2011 年 7 月 5 日
I prefer to use TEXTREAD in such cases. You could pass in different file names in every call and use the same matrix with different row numbers as output variables.
  2 件のコメント
Cody
Cody 2011 年 7 月 5 日
I read textread help but still do not know how to use textread to combine them. Could you please write down the detailed steps? Thanks.
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 5 日
What Nirmal meant was to use textread() to read each file and then combine them in MATLAB. You know it's very easy to concatenate data in MATLAB, do you?
a=[1 3 4 8]
b=[3 5 7 3]
c=[a;b]

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


Alex Z.
Alex Z. 2017 年 6 月 16 日
This can be done in Easymorph (it's free) using Append transformation. It can work with as many as 10 millions rows in the file.

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by