for-next loop parameter gets printed out

1 view (last 30 days)
Curious behavior. I have a matlab code with lots of for-next loops. One in particular that starts with
for I = J:LL
for some reason resulted in the LL value being printed out unexpectedly. None of my other for-next loops
did this. When I changed the statement to read
for I = J:LL;
i.e., I added a semicolorn to the end, LL was NOT printed out. This is worrisome because I'm not sure something
else has gone wrong. How does one track down what causes this behavior?
  2 Comments
dpb
dpb on 20 Aug 2022
Can you post a code snippet that exhibits the suspect behavior?
I've never seen it happen but I've not upgraded to a more recent release than R2020b yet, either, if somehow something got broken in an update.
Ken Bannister
Ken Bannister on 21 Aug 2022
Moved: Voss on 21 Aug 2022
Thank you. File attached.

Sign in to comment.

Accepted Answer

dpb
dpb on 21 Aug 2022
Edited: dpb on 21 Aug 2022
You've got a malformed for expression, the comma serves to make a second line of code on the same source line; the value of ll is echo'ed to the screen as asked for by this syntax.
for l = 1,ll
...
end
is syntactically the same as
for l = 1
ll
...
end
You undoubtedly intended to write
for l=1:ll
...
end
So, indeed, something else did go wrong besides; the loop only executed once instead of ll times since were no colon operators to set either step nor upper bound.
And, in this case the typo is also not a syntax error nor can the parser presume you didn't have some reason for only executing the loop once.
  6 Comments
Ken Bannister
Ken Bannister on 22 Aug 2022
OK, thanks again. I will continue working on it. Almost at the point where I should buy a FORTRAN package, e.g., Lahey or Absoft, and stay with it. As it is, I have a three-step process:
1. Convert f77 to f90 using what is at
https://fortran.uk/plusfortonline.php?id=140&id1=3&id2=1&id3=1&id4=-1&id5=1
2. Convert f90 to MATLAB using Ben Barrowes' f2matlab, note any errors
3. Cycle back to 1 and debug and update.
Then hope for the best. Doable, but will require patience and careful attention to details. The good news is that I have a confirmed listing of what the final output should look like.
dpb
dpb on 22 Aug 2022
Your test code before ran here with the correction noted; I don't know what answers should be, of course, but it ran to completion with the fix for the loop construct alone. There are a couple of warnings on suggested better syntax, but none that are of computational significance.
As for the conversion, I've not looked at the code much at all, but unless there were a huge amount of it and/or I didn't really know what it was trying to do, my first inclination would be to not do 1:1 translation but rewrite a MATLAB solution using MATLAB syntax and idioms instead of Fortran's.
The unfortunate decision by TMW to not support anything except Intel for mex is a real kick in the private parts that is, frankly, :just plain rude!" to long-term users but there seems to be no leverage to be able to use to get them to change course. Supporting the fortran compiler with gcc would be a major help and seems like should be essentially trivial to add to the existing support, or to simply just open up the interface and document the libraries and linking to let use any compiler. I fail to grasp the logic behind their current position. Anyways, as of now it appears that using Intel compiler is the only practical choice for going the mex route and they (Intel) have now also closed off that option except for "the high-priced spread" route.
It's an impasse, indeed...

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!