Simple question about merging two txt file?
3 次查看(过去 30 天)
显示 更早的评论
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
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
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!