Effacer les filtres
Effacer les filtres

join a specific column from csv 2 to csv 1

2 vues (au cours des 30 derniers jours)
Ant376
Ant376 le 6 Sep 2021
Commenté : Ant376 le 7 Sep 2021
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

Réponse acceptée

Dave B
Dave B le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by