MATLAB: HOW TO MERGE MANY .TXT FILES WITH A LOOP

commanddirectoryfilefilesloopmergetxt

Hello,
I have a question? How could I merge many txt files in one file with a loop? For example I have File1, File2, File3.txt, ….. , . in the same directory

Best Answer

File_all contains data of all file.
fid_p = fopen('File_all.txt','w'); % writing file id
x = 1:100;
for i =1:length(x)
filename = ['File',num2str(x(i)),'.txt'];%filename
fid_t=fopen(filename,'r');%open it and pass id to fscanf (reading file id)
data = fscanf(fid_t,'%c');%read data
fprintf(fid_p,'%c',data);%print data in File_all
fclose(fid_t);% close reading file id
end
fclose(fid_p); %close writing file id