Converting Fortran to MATLAB
8 views (last 30 days)
Gerrit Grundling on 16 Nov 2013
I have found a powerful yet compact Fortran code which I am trying to reprogram into MATLAB. Unfortunately, I have now hit a point where I need some help with the logic. The algorithms are easy enough to convert, so I'm not going to show them (unless you really want me to).
My problem is converting the Fortran "GOTO" logic into MATLAB. I have decided to study the sequence in which things happen, and from this created a map of triggers (e.g. if...goto 23 (else) goto 13) and I need to design my loops and functions around these. Some are simple enough (those in the green frames), but one of them (red frame) really has me baffled...
The nested loop between 15 and 19 is just three "for" loops, which are easily coded but less easily illustrated here. They are terminated by 16, 17 and 18 repectively.
From 28 on the calculations are done and some sequential post-processing takes place.
Thanks in advance
Walter Roberson on 16 Nov 2013
Use "continue" to resume back at point #13. Continue ends the current iteration of the enclosing loop and starts the next iteration.
Matters would be slightly more complicated if you needed to break out of the nested loops and go back to #13, but you do not need to do that so don't worry about it ;-)
More Answers (5)
Ben Barrowes on 17 Nov 2013
Have you tried the free spag (linux version) from Polyhedron software to clean up (refactor) some of these goto's for you? http://www.polyhedron.com/spag0html
If this doesn't refactor all of your goto's, at least it will get rid of many of them, often 50-75% of them. Spag also declares variables and prettifies the code to some extent so that the conversion to matlab becomes simpler.
If there are goto's left over, you can check out my goto remover (not ready for public consumption, so as yet unpublished) which removes all the remaining goto's using while/break/continue's: http://engineering.dartmouth.edu/~d30574x/consulting/consulting_gotorefactor.html
I would be happy to run your code through this and send it back to you.
Finally, you might try f2matlab at the file exchange to automatically convert the goto-less f90 style code to m-code: http://www.mathworks.com/matlabcentral/fileexchange/5260-f2matlab
f2matlab usually gets you 90-100% towards a working matlab code. You will have to test and debug the resulting code, though.
Sean de Wolski on 18 Nov 2013
You might be able to avoid reinventing the wheel by using the MEX api. This will let you compile your fortran code yuo have in a format that MATLAB can use.
Then you can just call this rather than rewriting it.