how to merge multiple csv files (or xlsx files)
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I have here, for example two csv files need to merge(merge into one). Can anyone kindly help how to merge them. Many thanks in advance.
0 Commenti
Risposte (1)
Walter Roberson
il 28 Gen 2016
Modificato: Walter Roberson
il 29 Gen 2016
fout = fopen('mergedData.csv', 'wt');
in_filenames = {'previouData.csv', 'currentData.csv'};
for K = 1 : length(in_filenames)
thisfile = in_filenames{K};
fin = fopen(thisfile, 'rt');
while true
thisline = fgetl(fin);
if ~ischar(thisline); break; end %end of file
if ~isempty(thisline); %assume empty lines are trailing empty lines
fprintf(fout, '%s\n', thisline);
end
end
fclose(fin);
end
fclose(fout);
Under some circumstances you can use much shorter code: you would need to be sure that the csv files were consistent about whether they used newline only or else carriage return + newline; and you would need to know that each file ended with a line terminator. The code I have given here is as long as it is because I do not assume that the line terminators are consistent and I do not assume that there is a line terminator at end of file.
4 Commenti
Walter Roberson
il 29 Gen 2016
It is not saved in a variable, it is saved in the file mergedData.csv as you asked to merge the files, not to read the files into memory.
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!