필터 지우기
필터 지우기

Merging multiples csv files in Matlab-R2014b

조회 수: 4 (최근 30일)
Mohammed Kamruzzaman
Mohammed Kamruzzaman 2015년 3월 11일
댓글: Brendan Hamm 2018년 2월 9일
I have 50 csv files without header. Size of each file is 11320x2 with one common column in all files. I would like to merge all of these files in matlab and would like to create a matrix of size (11320x51). Any suggestion is highly appreciated. Thank you!

채택된 답변

Ken Atwell
Ken Atwell 2015년 3월 11일
Assuming the files are all in the same folder and you have 'cd'ed to there, trying something like:
files = dir('*.csv'); % Get list of files
out = csvread(files(1).name); % First file
for ii = 2:numel(files)
new = csvread(files{ii}.name); % Read the nth file
out = horzcat(out, new[:,2]); % Concatenate "out" with the 2nd column from new file
end
This is from memory and may not be exactly right.
  댓글 수: 5
Renjith V Ravi
Renjith V Ravi 2018년 1월 28일
I am also getting the same error message "Unbalanced or unexpected parenthesis or bracket". Tried a lot to correct but couldn't.Please help me
Brendan Hamm
Brendan Hamm 2018년 2월 9일
In MATLAB you cannot index with [] as is done in the last line of the for loop. Change this to () and all will work fine.
files = dir('*.csv'); % Get list of files
out = csvread(files(1).name); % First file
for ii = 2:numel(files)
new = csvread(files{ii}.name); % Read the nth file
out = horzcat(out, new(:,2)); % Concatenate "out" with the 2nd column from new file
end

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

추가 답변 (2개)

Brendan Hamm
Brendan Hamm 2015년 3월 11일
Presumably you would want to use the csvread function. If all of the files are in the same directory, then set that as your working directory. This can be accomplished with
cd [directory]
where [directory] is the full path to your directory.
You can get a list of all of the files in the directory with the dir command.

Siach Xavisu
Siach Xavisu 2016년 12월 26일
You can merge them with relative ease with http://merge-csv.com even if they got headers.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by