Simple question about merging two txt file?

조회 수: 3 (최근 30일)
SEPIDEH
SEPIDEH 2016년 2월 3일
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
Ingrid
Ingrid 2016년 2월 3일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Subin Kuttappan Stellal Mary
Subin Kuttappan Stellal Mary 2016년 2월 9일
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);

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by