Combine multiple txt files into one single file with index

1 visualización (últimos 30 días)
sally_wu
sally_wu el 28 de Sept. de 2015
Editada: Cedric el 1 de Oct. de 2015
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 comentarios
Cedric
Cedric el 1 de Oct. de 2015
Editada: Cedric el 1 de Oct. de 2015
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).

Iniciar sesión para comentar.

Respuestas (1)

Madhav Rajan
Madhav Rajan el 1 de Oct. de 2015
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.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by