how to merge multiple csv files (or xlsx files)

1 visualizzazione (ultimi 30 giorni)
Mekala balaji
Mekala balaji il 28 Gen 2016
Commentato: Walter Roberson il 29 Gen 2016
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.

Risposte (1)

Walter Roberson
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
Mekala balaji
Mekala balaji il 29 Gen 2016
I still not getting any output. May I ask in which variable the final output(merged data) is saved.
Walter Roberson
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.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by