MATLAB Answers

reading multiple excel files

49 views (last 30 days)
I have afolder with 150 excel file and I want to read all of them one after another and save them in last in another excel sheet

  2 Comments

Azzi Abdelmalek
Azzi Abdelmalek on 7 Jun 2015
Can you give more details about your data in each Excell file
Ahmed Khalifa
Ahmed Khalifa on 7 Jun 2015
every excel file has one sheet with 4 columns A B C D

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 7 Jun 2015
folder='E:\fold1'
d=dir(folder)
e={d.name}
f=e(~cellfun(@isempty,regexp(e,'.+(?=\.xlsx)','match')))
for k=1:numel(f)-1
data{k,1}=xlsread(f{k})
end
M=cell2mat(data)
xlswrite('new_file',M)

  1 Comment

Ahmed Khalifa
Ahmed Khalifa on 7 Jun 2015
Thanks soooo much alittle nother question ,sorry if i have many excel files with 11 columns for every one and I want to read only column 5,6,7 and write them for all files in only one single file.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 7 Jun 2015
Don't use xlsread() for this unless you're prepared to wait a very long time. If you have Windows, you can use ActiveX and get this done in a few seconds. With xlsread(), each time you call it, it has to launch Excel, open your workbook, haul over the data (probably using ActiveX), and finally shut down Excel. With ActiveX, you launch Excel just once, and shut it down just once, so it will be about 150 times faster for 150 workbooks. I attach a demo on how to read and write Excel files with ActiveX. If I have more than 2 files, I never use xlsread or xlswrite - I use ActiveX. If I want more precise control over formatting (decimal places, alignment, coloring, borders, etc.) I use ActiveX, or use a template. It's not hard to learn so don't be afraid - it's just like any other object oriented programming you're used to.

  2 Comments

Ahmed Khalifa
Ahmed Khalifa on 7 Jun 2015
well, thank you first but I need it to be in matlab cause it will be asmall step in alarge program so it's just for sequence of the whole job.
Image Analyst
Image Analyst on 7 Jun 2015
I guess you didn't run my demo and notice that it is in a MATLAB m-file and is, in fact, MATLAB code. It's just as much MATLAB code as calling xlsread() or xlswrite() is. Here is a link to MATLAB documentation on actxgetrunningserver http://www.mathworks.com/help/matlab/ref/actxgetrunningserver.html?s_tid=srchtitle You see, it's a MATLAB function and you don't have to feel guilty about using it. I mean, after all, xlsread() uses it, so why can't you ?

Sign in to comment.

Sign in to answer this question.