Simple question about merging two txt file?

3 visualizaciones (últimos 30 días)
SEPIDEH
SEPIDEH el 3 de Feb. de 2016
Hi all,
I have two text files, each of them has two columns, I want to prepare another txt file which should contain two columns:(first column will have the same value like the first column of one of the txt files, and second column should be summation of cells from "rp1" + "rp2". I give the example below:
First column Second column
0 990 [which is summation of the first value of second column from each txt files::789+201]
0.0416 988 [which is summation of the first value of second column from each txt files::789+199] 0.083 987 [which is summation of the first value of second column from each txt files::789+198] . . . .
Hope to hear your answers. Thanks for help in advance Sepideh
  1 comentario
Ingrid
Ingrid el 3 de Feb. de 2016
where do you have problems? what is not working? Do you not succeed in reading in the data, in summing the data, or in writing the data in a txt file? Please show us some code that you have tried so far and indicate the error that you encounter so that we can assist you further

Iniciar sesión para comentar.

Respuestas (1)

Subin Kuttappan Stellal Mary
Subin Kuttappan Stellal Mary el 9 de Feb. de 2016
You can do this by reading the contents of the txt files to arrays, merging them, and saving to another txt file. Following code accomplished this task :
formatSpec = '%f %d';
siz = [2 Inf];
fileID = fopen('rp1.txt','r');
A = fscanf(fileID,formatSpec,siz);
A = A';
fclose(fileID);
fileID2 = fopen('rp2.txt','r');
B = fscanf(fileID2,formatSpec,siz);
B = B';
fclose(fileID);
C = [A(:,1) A(:,2) + B(:,2)];
C = C';
fileID3 = fopen('rp3.txt','w');
fprintf(fileID3,'%0.4f %d\n',C);
fclose(fileID3);

Community Treasure Hunt

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

Start Hunting!

Translated by