Borrar filtros
Borrar filtros

Merge multiple .csv files of different dimensions into one data

7 visualizaciones (últimos 30 días)
Curious Mind
Curious Mind el 20 de Feb. de 2018
Comentada: Curious Mind el 20 de Feb. de 2018
Hi all: Say I have about 200 .csv files of different dimensions (e.g 6662*2 double and 6634*2 double) and I would like to merge them into one dataset. I have a code that can merge multiple .csv files of same dimensions but not of varying dimensions like one described above. Is there a code that can help me do that? Thank you.
  1 comentario
Bob Thompson
Bob Thompson el 20 de Feb. de 2018
Do they all share the same second dimension? If so you should just be able to concatenate them.
data = [];
for i=1:numberoffiles
data = [data,csvread(file(i))];
end
You might have to finesse things a little bit to make sure the right dimensions are being joined, but that's the general idea.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 20 de Feb. de 2018
Assuming all the files have the same number of columns (otherwise you need to explain what to do with the files with less columns):
filepath = 'C:\somewhere';
filelist = {'file.csv', 'file2.csv', ..., 'filen.csv'}; %obtained any which way you want. Maybe with dir
filecontents = cell(numel(filelist), 1);
for fileidx = 1:numel(filelist);
filecontents{fileidx} = csvread(fullfile(filepath, filelist{fileidx})); %or use dlmread
end
concatenatedcontent = vertcat(filecontents{:});
csvwrite('c:\somewhere\somefile.csv', concatenatedcontent);
  1 comentario
Curious Mind
Curious Mind el 20 de Feb. de 2018
the csv files have the same number of columns but have different dimensions.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by