How to read several excel files (30 files) without repeating the program?
Show older comments
Hi Everyone;
I just wrote a program to read from one excel file and then calculate something by using the column data. Now I want to extend it to read 30 excel files and do exactly the same thing (I mean calculate same column in each files). The question is I have to copy all the program and change the name of each line which is too difficult so I wonder to know if another way that I can examine. Could you please help me?
Accepted Answer
More Answers (3)
Mark Whirdy
on 19 Dec 2012
Edited: Mark Whirdy
on 19 Dec 2012
Hi Ara, what does "change the name of each line" mean? Can you provide some sample code to help would-be assisters understand the problem more clearly? Generally, the more effort you put into the question, the better the answer you'll get.
My guess is that you want to loop through a cell-array of excel filenames, opening each file in turn and reading in its contents ... is this right?
myPath = 'C:\';
myFileArray = {'myfile1.xlsx';'myfile30.xlsx'};
exl = actxGetRunningServer('Excel.Application');
exlWkbks = exl.Workbooks;
for i = 1:size(myFileArray,1)
tempWkbook = exlWkbks.Open(fullfile(myPath,myFileArray{i,:}));
tempSheet = tempWkbook.ActiveSheet;
tempArray = tempSheet.Range('A1:A100').Value;
tempWkbook.Close;
% myVector = cell2mat(tempArray)*2; % PERFORM SOME OPERATION HERE
end
another link may help with excel activex properties/methods http://www.mathworks.co.uk/matlabcentral/answers/42710-problem-with-xlsread-bizarre-results
besides this, google "matlab excel actxserver"
5 Comments
Ara
on 19 Dec 2012
Mark Whirdy
on 19 Dec 2012
Edited: Mark Whirdy
on 19 Dec 2012
So putting this snippet inside a loop (as per my post above) doesn't provide a solution? Populate a matrix "s4" then iteratively (as below)with your required data from each file, and plot outside the loop.
What am I missing here?
n = 50; % how many rows in '1615_0.xls', do all files contain the same rows?
s4 = NaN(n,30);
for i = 1:...
s4(:,i) = sqrt(s4r.^2-s4cor.^2); % "i" increments on each loope (i.e. each file)
end
plot(s4);
Mark Whirdy
on 19 Dec 2012
Edited: Mark Whirdy
on 19 Dec 2012
I think we have a language barrier problem. Can you start with the code I have posted above and then adapt it, I can't write this for you as I don't have your excel files.
In terms of this line: ...
data = xlsread{'1608_1.xls','A120:AF3723'; '1616_5.xls', 'A120:AF3723'};
1) you need round brackets, not chain brackets
2) have you looked at xlsread helpfile?, as your arguments do not conform to the required inputs [xlsread(filename,sheet,range)], you're referencing several files in the same xlsread.
Azzi Abdelmalek
on 19 Dec 2012
fic=struct2cell(dir('yourfolder/*.xls'))
file=fic(1,:)
for k=1:numel(file)
data{k}=xlsread(file{k})
end
6 Comments
Image Analyst
on 19 Dec 2012
This will work but take a very long time since you'd have to launch and shutdown Excel 30 times. Better to use ActiveX.
Ara
on 19 Dec 2012
Image Analyst
on 19 Dec 2012
Edited: Image Analyst
on 19 Dec 2012
I know - just like I said. You can try control-c to stop the running m-file. Click in the command window first and then try typing control-c a bunch of times. Otherwise you may have to use control-shift-Esc and kill Excel or MATLAB via the Process list. Next time use ActiveX if you have more than about two Excel workbooks that you need to work with - don't use xlsread. Let me know if you need a demo.
Ara
on 19 Dec 2012
Image Analyst
on 19 Dec 2012
Not sure if you were replying to me when you asked for a demo. I offered an ActiveX demo but then you seem insistent on using xlsread which will take an eternity. Mark Whirdy gave you some ActiveX code that you can use instead of xlsread.
Ara
on 19 Dec 2012
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!