How to read several excel files (30 files) without repeating the program?
이전 댓글 표시
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?
채택된 답변
추가 답변 (3개)
Mark Whirdy
2012년 12월 19일
편집: Mark Whirdy
2012년 12월 19일
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
Ara
2012년 12월 19일
Mark Whirdy
2012년 12월 19일
편집: Mark Whirdy
2012년 12월 19일
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
2012년 12월 19일
편집: Mark Whirdy
2012년 12월 19일
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
2012년 12월 19일
fic=struct2cell(dir('yourfolder/*.xls'))
file=fic(1,:)
for k=1:numel(file)
data{k}=xlsread(file{k})
end
댓글 수: 6
Image Analyst
2012년 12월 19일
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
2012년 12월 19일
Image Analyst
2012년 12월 19일
편집: Image Analyst
2012년 12월 19일
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
2012년 12월 19일
Image Analyst
2012년 12월 19일
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
2012년 12월 19일
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!