combining different csv files from a folder into one matrix
Show older comments
Hello, I have 800 files with 1554rows and 2columns so (1554x2) , that I'd like to read from a folder and then combine them into a large matrix of 1554x801. The first column is common to each file, and so I'd like to set only the first column in the final matrix as the same column of all the files, and then the subsequent columns as the second column of each of the 800 files. I hope this makes sense.
Example:
A.csv=[1 2; 4 5] and B.csv=[1 8 ;4 7], C.csv=[1 6;4 10]
I'd like to combine these into
Combined=[1 2 8 6; 4 5 7 10]
Would anyone possibly have a code for this? I'm new to Matlab and have tried a number of codes that merge files. but they stack them horizontally so I get
[1 2;4 5;1 8;4 7;1 6;4 10].
Thank you!
Accepted Answer
More Answers (1)
KSSV
on 22 May 2019
csvfiles = dir('*.csv') ;
iwant = zeros(1554,801) ;
for i = 1:length(csvfiles)
data = csvread(csvfiles(i).name) ;
if i == 1
iwant(:,1:2) = data ;
else
iwant(:,i+1) = data(:,2) ;
end
end
8 Comments
Michel Nieuwoudt
on 22 May 2019
KSSV
on 22 May 2019
It sems you have some header lines in your file? Attach one file.
Michel Nieuwoudt
on 22 May 2019
Michel Nieuwoudt
on 22 May 2019
Michel Nieuwoudt
on 22 May 2019
KSSV
on 22 May 2019
It can be but...why unnecessary complexity? You should take it as a table.
Michel Nieuwoudt
on 22 May 2019
Categories
Find more on Text Files 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!