How to copy and paste multiple columns from different tables to the destination table in excel.
4 views (last 30 days)
Show older comments
Upendra Tuladhar
on 17 May 2021
Commented: Upendra Tuladhar
on 17 May 2021
I have large number of CSV files (more than 1000) and need to copy first column from each tables to a single table. I am not sure how to write them in separate columns in destination excel file. I have managed to isolate the first column from all the csv files but couldn't find any solution to write them in different columns. This code writes all the columns in the first column of output file. Is there any way to write them in the column next to the previous one?
clc
clear all
csvFiles = dir('*.csv');
numfiles = length(csvFiles);
mydata = cell(1, numfiles);
for k = 1:numfiles
mydata{k} = readtable(csvFiles(k).name);
T = mydata{k}(:,1)
writetable(T,'myData.xls','Sheet',1,'Range','A1')
end
0 Comments
Accepted Answer
KSSV
on 17 May 2021
I am assuming that the length of column in each csv file is same.
clc
clear all
csvFiles = dir('*.csv');
numfiles = length(csvFiles);
mydata = cell(1, numfiles);
A = zeros([].numFiles) ;
for k = 1:numfiles
mydata{k} = readtable(csvFiles(k).name);
T = mydata{k}(:,1) ;
A(:,k) = T ;
end
T = array2table(A) ;
writetable(T,'myData.xls')
You can also use writematrix function.
More Answers (0)
See Also
Categories
Find more on Data Import from MATLAB 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!