Combining 12 .csv Files

29 visualizzazioni (ultimi 30 giorni)
Rachel Cox
Rachel Cox il 16 Nov 2022
Risposto: Aritra il 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.

Risposte (1)

Aritra
Aritra il 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');

Categorie

Scopri di più su Printing and Saving 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