Combine multiple txt files into one single file with index

1 回表示 (過去 30 日間)
sally_wu
sally_wu 2015 年 9 月 28 日
編集済み: Cedric 2015 年 10 月 1 日
I have five output txt files (Apple, Bean, Carrot, Tomato, and Orange) with one column and index as it shown below:
And now I need to combine this single column output file into multiple files as one output file in txt form and have the following output file with the following index and column:
How can I do that?
  2 件のコメント
Cedric
Cedric 2015 年 10 月 1 日
編集済み: Cedric 2015 年 10 月 1 日
Try using TEXTSCAN to read your files. Something like:
fNames = {'file1.txt', 'file2.txt', .. } ;
data = cell( size( fNames )) ;
for k = 1 : numel( fNames )
fId = fopen( fNames{k}, 'r' ) ;
content = textscan( fId, '%s' ) ;
data{k} = content{1} ;
fclose( fId ) ;
end
Work on this until it loads the data properly and see if you can build the output from this data cell array (and e.g. my answer to one of your previous questions).

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

回答 (1 件)

Madhav Rajan
Madhav Rajan 2015 年 10 月 1 日
I understand that you want to import files based on index. You can use the import tool by right clicking on one text file and importing the data into a cell array of type text. You can specify the format and generate a function for one file. Then you can call this function for all files in a for loop.
You can access the elements of the cell using the '{}' notation. You can then restructure the cells in the appropriate way and concatenate the columns of the other files together into one variable. Then you can save the data to a file in MATLAB.
Hope this helps.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by