Read multiple excel file and write into single excel file

2 views (last 30 days)
Hi and very good day,
I do have a 100 of excel files and save as thermo1.xls, thermo2.xls......thermo100.xls. Each of the files consist of a row and 20 columns of data on the single sheet (it write on A1 to T1).
The thing that i need to do is, to write all of the data (A1:T1 on thermo1.xls, A1:T1 on thermo2.xls.....) into a single sheet on a new single excel file (it might be saved as finalthermo.xls).
thus, finally the data will be sort (on finalthermo.xls) as (A1:T1 from thermo1.xls, A2:T2 from thermo2.xls......A100:T100 from thermo100.xls)
At the moment, i wrote this code by it wont happen as i wish, all entire row and column were overwrite with data from the last file (thermo100.xls)
source_dir = 'C:\Users\sourcedir.....' %my file source dir
dest_dir = 'C:\Users\destdir.......' %my file dest dir
source_files = dir(fullfile(source_dir, '*.xls'));
for i = 1:length(source_files)
data = xlsread(fullfile(source_dir, source_files(i).name));
for k =1:100
xlswrite('finalthermo.xls',data, 1, sprintf('A%d',(k+1)-1));
end
end
I looking forward for somebody to help me fixing the coding...
thank you.

Accepted Answer

Renato Agurto
Renato Agurto on 6 Jan 2016
hello
you can try this.
source_dir = 'C:\Users\sourcedir.....' %my file source dir
dest_dir = 'C:\Users\destdir.......' %my file dest dir
source_files = dir(fullfile(source_dir, '*.xls'));
data = zeros(100,20)
for i = 1:length(source_files)
data(i,:) = xlsread(fullfile(source_dir, source_files(i).name));
%or to make sure you only read one line
%data(i,:) = xlsread(fullfile(source_dir, source_files(i).name),1,'A1:T1');
end
xlswrite('finalthermo.xls',data);
  1 Comment
Zaki Ikhsan
Zaki Ikhsan on 6 Jan 2016
Hello Renato
I run on your code, thank you very much, it works perfectly! :) appreciate it.
Upvote and thumbs up for you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!