For Loop in Command Window Not Working - But Code Works Line-By-Line?
5 views (last 30 days)
Show older comments
Hi, I am trying to run a for loop in the Matlab command window to open, edit, and save new copies of a group of 217 files I have.
When I run the code within the for loop line-by-line, it works perfectly. When I copy and paste the whole block of code with the for loop included in the command window, no files are opened, edited, or saved.
Any ideas on why the loop wouldn't work?
L7 = Band ('http://fsf.nerc.ac.uk/user_group/bands/Landsat7TM.xml')
for i = 1:217;
str = ['C:\',num2str(i),'.txt']
str2 = ['C:\',num2str(i),'.xlsx']
s = importasd(str)
t = convolve(s, L7)
fields = {'name', 'datetime', 'header', 'wavelength'};
t = rmfield(t,fields)
o = struct2table(t)
writetable(o,str2)
End
0 Comments
Accepted Answer
dpb
on 2 Oct 2015
Edited: dpb
on 2 Oct 2015
L7 is just a character string; what do you expect of a convolution of it with an (assumed) data file returned as s? Seems to make no sense...
The key word closing the loop is 'end' not 'End'; Matlab is case-sensitive. Hence the loop construct hasn't yet been closed so nothing happens--Matlab is awaiting instructions to actually begin the loop at this point. Of course, then it's going to error on 'End'
>> for i=1:5,i,End % Nothing happens here until...
end
i =
1
Undefined function or variable 'End'.
>>
2 Comments
dpb
on 3 Oct 2015
It's not the for loop per se; there's no limit there in Matlab (other than the size of an index variable in a counted loop, perhaps). I would presume the error referring to an "event" is related to the interprocess communication with Excel and resources required for that connection. I suggest submitting this to TMW at www.mathworks.com to see if there's a workaround or some way they can improve the implementation.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!