Azzera filtri
Azzera filtri

join a specific column from csv 2 to csv 1

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Dave B
Dave B il 7 Set 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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by