How to merge multiple xls files into a single file with the same worksheet?
1 view (last 30 days)
Show older comments
Kasih Ditaningtyas Sari Pratiwi
on 11 Nov 2017
Commented: Kasih Ditaningtyas Sari Pratiwi
on 12 Nov 2017
Hi. I want to ask about how to merge multiple xls files into a single file with the same worksheet. I read some questions which also have the same problem, but mostly the resulted file has separate worksheets. In my case, I want to merge the table so that it only has one worksheet. I try this code below:
[files,folder] = uigetfile('*.xls','Select Files','MultiSelect','on');
output = fullfile(folder,'rainfall.xls');
addpath(dir);
for i = 1:numel(files)
ftab = readtable(files{i});
writetable(ftab,output,'Sheet',files{i});
end
but of course it has separated worksheets. I want to make it in one worksheet as the files have also the same structure so it should be easily to merge. Could you please help me? I really appreciate for your help.
0 Comments
Answers (1)
dpb
on 12 Nov 2017
Probably simplest presuming the individual sheets aren't terribly big is to loop through and accumulate all the data into one array and then write it at the end to the target worksheet. This is minimum number of nFiles reads and one write.
Otherwise, to do sequentially you'll have to keep track of location in the sheet to write each subsequent dataset to and it's both nFiles reads and writes.
See Also
Categories
Find more on Spreadsheets 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!