MATLAB: How to merge two text files in one

merge

hi..I have two text files and i want to merge into one output file,also some nan values are present in this files and i want to delete from it.kindly help me.Sample files are attached here.

Best Answer

Simplest here is probably just brute force...open an output file, then open each input in succession, copying lines (excepting for the first, without the header lines) from input to the output making in text substitutions need along the way.
You could, of course, read each file into memory, do the clean up there and then write a formatted file. If the input files are truly huge this may be worth doing but the other is quick 'n dirty for reasonable file sizes...
fidO=fopen('output.txt','w');
fid=fopen('input1.txt','r');
while ~feof(fid)
for i=1:2 % do header lines
l=fgets(fid);
fprintf(fidO,'%s',l);
end
l=strrep(fgets(fid),'-99.9',blanks(5)); % floating missing value

l=strrep(l,'-99',blanks(3)); % integer missing value

fprintf(fidO,'%s',l);
end
fid=fclose(fid); % done with first input
fid=fopen('input2.txt','r');
for i=1:2, fgets(fid); end % skip header lines
while ~feof(fid)
l=strrep(fgets(fid),'-99.9',blanks(5)); % floating missing value
l=strrep(l,'-99',blanks(3)); % integer missing value
fprintf(fidO,'%s',l);
end
fclose('all')
clear fid*
You'll note above for just two files I didn't even bother to create the outer loop over them; for more it would be worth doing that refinement.