For Loop in Command Window Not Working - But Code Works Line-By-Line?

5 views (last 30 days)
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

Accepted Answer

dpb
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
Mr. Meeseeks
Mr. Meeseeks on 3 Oct 2015
Edited: Mr. Meeseeks on 3 Oct 2015
L7 uses a function called 'band' to call data from the XML file, the convolution uses the data in that XML file.
I didn't realize Matlab was case sensitive, thank you for that. The loop runs now.
However, after on the 200th iteration, I get an error using writetable. The error message is as follows:
Error using writetable (line 106)
Error writing table variable 'data' to 'C:\Output200.xlsx':
Error registering event(s), Advise
Is there a limit to the number of times a loop can run? I can't imagine any other reason this error would occur after 199 iterations.
EDIT: I created a second loop that runs from iterations 200 to 217 and it works fine. I guess there is some kind of limit to the number of times writetable can run in one loop.
dpb
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.

Sign in to comment.

More Answers (0)

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!