How do I use xlsread in a for loop?
4 次查看(过去 30 天)
显示 更早的评论
Hello, I've read several responses on this site vaguely related to my question, but I'm a pretty new user so they didn't really help me much. All I'm trying to do is to get Matlab to take some .xlsx files and assign them to matrices. There are 14 files in my specified folder (the same one the matlab code is running in) and they are named File1.xlsx - File14.xlsx. So far, I've failed miserably to get matlab to even recognize that these files exist when I run xlsread within a simple for loop. I figured I'd worry about the matrix assignments later, yes I know this will just reassign this to matrix over and over. Here's my code so far.
for i = 1:14
bob = strcat('File',i,'.xlsx')
matrix = xlsread(bob);
end
With output:
Error using xlsread (line 128)
XLSREAD unable to open file 'File.xlsx'.
File '/Users/timmoreno/Desktop/KLH Data Formatted/File.xlsx' not found.
Error in ReadTest1 (line 6)
matrix = xlsread(bob);
Notice that it completely ignores that i = 1, but even if I put in strcat('File1',i,'.xlsx') it still fails to see the file, which is there, and works if I use xlsread on File1 outside of a for loop. What the heck is happening?
I'm grateful for any help, I should also mention that I'm running Matlab 2015.
0 个评论
采纳的回答
Star Strider
2015-8-17
There are several ways to do what you want, one is:
bob = sprintf('File%d.xlsx',i);
It depends on how your files are named. If the numbers include leading zeros, consider:
bob = sprintf('File%02d.xlsx',i);
or something similar.
3 个评论
Joseph Porter
2017-4-23
I need to do this for sheet numbers within my xls read. my code is the same as OP but the solution doesn't work,I can only assume as sheet number is a string within '' markers. any thoughts how to loop sheet number in xlsread? thank you
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!