Merge multiple .csv files of different dimensions into one data

3 ビュー (過去 30 日間)
Curious Mind
Curious Mind 2018 年 2 月 20 日
コメント済み: Curious Mind 2018 年 2 月 20 日
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);
  1 件のコメント
Curious Mind
Curious Mind 2018 年 2 月 20 日
the csv files have the same number of columns but have different dimensions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by