Combine multiple txt files into one single file with index
1 次查看(过去 30 天)
显示 更早的评论
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
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
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.
You can refer the following links for more information: http://www.mathworks.com/help/matlab/ref/importtool-app.html http://www.mathworks.com/help/matlab/ref/cell.html
Hope this helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!