MATLAB: Merging text data from sequence data files in a folder

fprintfMATLAB

Hello, here is my script to merge data into a single file.
files=dir('*.data');
fileout='merged.data';
fout=fopen(fileout,'w');
for cntfiles=1:length(files)
fin=fopen(files(cntfiles).name);
linecount = 0;
while ~feof(fin)
linecount = linecount + 1;
linetext = fgetl(fin);
if linecount >= 10 % Nine (9) header lines being skipped
fprintf(fout, '%s %d\n', linetext, cntfiles);
end
end
fclose(fin);
end
fclose(fout);
Original data structure is like this:
ITEM: TIMESTEP
1000
ITEM: NUMBER OF ATOMS
362
ITEM: BOX BOUNDS mm mm mm
-1.1 1.1
-1.00019 1.9
-0.75007 0.75007
ITEM: ATOMS id type type x y z ix iy iz vx vy vz fx fy fz omegax omegay omegaz radius
1 1 1 0.450082 1.76687 -0.101338 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324
2 1 1 -0.0949301 1.76202 -0.0205438 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324
3 1 1 0.0403266 1.78975 0.0742209 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324
The code works well to merge the individual .data files but the resulting merged.data have an extra column at the end (20 column) with the id of the data input. For instance, below is the point of interface of data from two files;
360 1 1 -0.146193 1.86848 0.0697032 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0189 1
361 1 1 -0.265613 1.86954 0.0197866 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0189 1
362 1 1 0.217502 1.7725 0.0649903 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0189 1
1 1 1 0.450082 1.75687 -0.101338 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324 2
2 1 1 -0.0949301 1.75202 -0.0205438 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324 2
3 1 1 0.0403266 1.77975 0.0742209 0 0 0 -0 -1 -0 0 0 0 0 0 0 0.0324 2
Can you kindly help to address this. Thank you.

Best Answer

In your 'fprintf' statement, you are printing the index of the file
fprintf(fout, '%s %d\n', linetext, cntfiles);
If you don't want the file index to appear in the merged file, modify the statement to:
fprintf(fout, '%s\n', linetext);
Related Question