MATLAB: Loading multiple excel files in a loop

excelfaq4.12importMATLAB

I have thousands of excel files that contain data in a 1440x~35 array. I only need a few columns in each file. The excel files are logically ordered with the pattern 1018ahua.xls, 1019ahua.xls, etc. I have tried loading columns in a loop using the xlsread command in a variety of ways and am always unable to do it. I use Matlab R2010b. Can anyone help me?

Best Answer

Hi,
first of all doing a xlsread in a loop is a bad idea since you always open and close the EXCEL COM Server. You have to do it manually in a better way, like:
%open Excel

ex = actxserver('excel.application');
for i=1:thousands_of_excel_files
ex.Workbooks.Open('C:\absolut_path_to file\filename.xls')
out = get(ex.Range('B3:D6'),'Value')
end
ex.Quit
So if its really ordered like 1018ahua.xls, 1019ahua.xls and so on you can do
%open Excel
ex = actxserver('excel.application');
for i=1018:thousands_of_excel_files
ex.Workbooks.Open(['C:\absolut_path_to file\',num2str(i),'ahua.xls'])
out = get(ex.Range('B3:D6'),'Value')
end
ex.Quit