Merge multiple .csv files of different dimensions into one data

7 次查看(过去 30 天)
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 个评论
Bob Thompson
Bob Thompson 2018-2-20
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.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2018-2-20
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);

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by