how to use xlsread for creating new matrices?
1 次查看(过去 30 天)
显示 更早的评论
I have 1000 excel files (named H) and need to import the parameters of sheet 2 (named Q) to 1000 new matrices in matlab for analysis. i used this code in command window and got answer: Q1=xlsread('H(1)','Q'). but when i use the following code in m.file it doesn't work. could you help me to correct my code? thank you in advance
for i=1:1000
Q1(i)=xlsread('H(i)','Q');
end
0 个评论
回答(2 个)
Image Analyst
2014-11-21
Geoff told you how to prepare the filename and that's good.
If you don't want this to take forever, because it will have to launch Excel 1000 times and shutdown Excel 1000 times, you should use ActiveX instead of xlsread(). You launch Excel once, open all the workbooks, then shutdown Excel once. It will be a lot faster. I attach a demo where I do it on one or two workbooks.
Geoff Hayes
2014-11-21
Maryam - please clarify what you mean by your 1000 files being named H. Are your files named H(1).xlsx, H(2).xlsx, etc.? If so, then you would build your file names using sprintf as
for k=1:1000
filename = sprintf('H(%d).xlsx',k);
end
If the matrices from each Q worksheet are of different dimensions, then you will want to use a cell array to store all of the data. Something like
Q = cell(1000,1);
for k=1:1000
filename = sprintf('H(%d).xlsx',k);
Q{k} = xlsread(filename,'Q');
end
Try the above and see what happens!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!