Clear Filters
Clear Filters

join a specific column from csv 2 to csv 1

2 views (last 30 days)
i have a folder containing 10 files, a1.csv, a2.csv ... a5.csv and b1.csv, b2.csv ... b5.csv
I am trying to create a new csv file with the columns from .csv A and the second column from .csv B
folderPath = '/Volumes/Extreme SSD/ERA5/csv/';
for y = 1:5
yStr = num2str(y);
A = csvread([folderPath,'a',yStr,'.csv']);
B = csvread([folderPath,'b',yStr,'.csv']);
C = [A,B(2)];
csvfilename = 'c'+string(y)+'.csv';
csvwrite(csvfilename,C);
end
I keep getting the message:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
with the above, what am i doing wrong?
edit to add:
When i use
C=[A,B];
there is no error

Accepted Answer

Dave B
Dave B on 7 Sep 2021
Assuming your CSV files are shaped appropriately, you probably want:
C = [A B(:,2)]
B(2) refers to the second element in B, B(:,2) refers to the second column in B.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!