Azzera filtri
Azzera filtri

Merge multiple .csv files of different dimensions into one data

7 visualizzazioni (ultimi 30 giorni)
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 Commento
Bob Thompson
Bob Thompson il 20 Feb 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.

Accedi per commentare.

Risposte (1)

Guillaume
Guillaume il 20 Feb 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);

Categorie

Scopri di più su File Operations 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!

Translated by