Combine multiple txt files into one single file with index

조회 수: 7 (최근 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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by