Combining 12 .csv Files

30 views (last 30 days)
Rachel Cox
Rachel Cox on 16 Nov 2022
Answered: Aritra on 21 Nov 2022
Helllo, I'm trying to combine 12 .csv files into one. They all have the same column titles but are 12 seperate months. We want to be able to plot all of the months in order on one graph.

Answers (1)

Aritra
Aritra on 21 Nov 2022
Hi Rachel,
As per my understanding, you are trying to combine the data from multiple ‘.csv’ files into a single ‘.csv’ file.To solve this issue, you can use the readmatrix(filename) function for reading column-oriented data from ‘.csv’ files. Next the horzcat(A,B) function can be used for concatenating the matrices horizontally.
Assuming the files are all in the same folder and you have changed your directory to there, try something like:
files = dir('*.csv'); % Get list of files
out = readmatrix(files(1).name,delimitedTextImportOptions('DataLines',[1,Inf])); % First file
for ii = 2:numel(files)
new = readmatrix(files(ii).name,delimitedTextImportOptions('DataLines',[1,Inf])); % Read the nth file
out = horzcat(out,new(:,:)); % Horizontal Concatenation
end
writecell(out,'Output.csv');

Categories

Find more on Printing and Saving 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!