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!