join a specific column from csv 2 to csv 1

2 次查看(过去 30 天)
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

采纳的回答

Dave B
Dave B 2021-9-7
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by