Effacer les filtres
Effacer les filtres

Combining 12 .csv Files

33 vues (au cours des 30 derniers jours)
Rachel Cox
Rachel Cox le 16 Nov 2022
Réponse apportée : Aritra le 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.
  1 commentaire
Stephen23
Stephen23 le 16 Nov 2022
What have you tried so far?

Connectez-vous pour commenter.

Réponses (1)

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

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by