How to merge multiple xls files into a single file with the same worksheet?

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

回答(1 个)

dpb
dpb 2017-11-12
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.

Community Treasure Hunt

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

Start Hunting!

Translated by