Combining 12 .csv Files

조회 수: 12 (최근 30일)
Rachel Cox
Rachel Cox 2022년 11월 16일
답변: Aritra 2022년 11월 21일
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
Stephen23
Stephen23 2022년 11월 16일
What have you tried so far?

댓글을 달려면 로그인하십시오.

답변 (1개)

Aritra
Aritra 2022년 11월 21일
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');

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by