HOW TO MERGE MANY .TXT FILES WITH A LOOP?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Mehmed Saad
le 20 Avr 2020
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
5 commentaires
Rik
le 10 Juin 2020
You can add a newline:
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
fprintf(fid_p,'\n');%add newline
end
fclose(fid_p); %close writing file id
Adam Danz
le 10 Juin 2020
Ivan Mich, don't forget to accept the answers to questions that were helpful to you. You've got lots of answers but few are accepted. If the answers weren't helpful, leave feedback so future visitors can benefit from the dialog. Here's a list of your questions.
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!