Merge multiple .csv files of different dimensions into one data
3 views (last 30 days)
Show older comments
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 Comment
Bob Thompson
on 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.
Answers (1)
Guillaume
on 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);
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!