how to merge multiple csv files (or xlsx files)

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.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de En. de 2016
Editada: Walter Roberson el 29 de En. de 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 comentarios

Mekala balaji
Mekala balaji el 29 de En. de 2016
Sir/Madam,
I do not get any output of merged data.
Best regards,
Walter Roberson
Walter Roberson el 29 de En. de 2016
I have edited to correct a mistake.
Mekala balaji
Mekala balaji el 29 de En. de 2016
I still not getting any output. May I ask in which variable the final output(merged data) is saved.
Walter Roberson
Walter Roberson el 29 de En. de 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.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 28 de En. de 2016

Comentada:

el 29 de En. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by