Converting Fortran to MATLAB

조회 수: 7 (최근 30일)
Gerrit Grundling
Gerrit Grundling 2013년 11월 16일
댓글: Ben Barrowes 2013년 12월 2일
Greetings, all
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
Walter Roberson 2013년 11월 16일
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 ;-)
  댓글 수: 1
Gerrit Grundling
Gerrit Grundling 2013년 11월 19일
Thanks.
This is new to me, but I'll try that.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Ben Barrowes
Ben Barrowes 2013년 11월 17일
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.
Good luck!
  댓글 수: 1
Gerrit Grundling
Gerrit Grundling 2013년 11월 20일
Thanks, I've finally run enough of it through SPAG to navigate myself through.
I've attached the SPAG result as a text file, starting at line 102.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2013년 11월 18일
Gerrit,
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.
  댓글 수: 1
Gerrit Grundling
Gerrit Grundling 2013년 11월 19일
I have to rewrite it eventually, because I need more control over the inputs.
Thanks, though.

댓글을 달려면 로그인하십시오.


Gerrit Grundling
Gerrit Grundling 2013년 11월 18일
Greetings, Ben
I tried SPAG, but it seems that, even after getting rid of the comments and the excess, operations 13 to 28 are too many lines to handle. I'll have to buy the package, but not at that price.
Thanks, though.
  댓글 수: 1
Ben Barrowes
Ben Barrowes 2013년 12월 2일
The evaluation version of spag is free and can handle any number of lines. It will get rid of goto's, but will not change over to f90 format. You might try that version:
They have windows, linux, and Mac evaluation versions.

댓글을 달려면 로그인하십시오.


Gerrit Grundling
Gerrit Grundling 2013년 11월 18일
Greetings, Walter
Colour me crazy, but doesn't "continue", erm, do nothing...? (In Matlab as it is in Fortran).
  댓글 수: 2
James Tursa
James Tursa 2013년 11월 18일
CONTINUE does not have the same meaning in MATLAB and Fortran:
Do nothing:
MATLAB: ;
Fortran: continue
Skip the rest of the current iteration and go to next iteration:
MATLAB: continue
Fortran: cycle
Gerrit Grundling
Gerrit Grundling 2013년 11월 19일
Thanks for that explanation, your answer has led me to this curious graphic illustration:
I think I'll try this then.

댓글을 달려면 로그인하십시오.


Gerrit Grundling
Gerrit Grundling 2013년 11월 25일
편집: Gerrit Grundling 2013년 11월 25일
I have had a moment of inspiration, and decided to rearrange the sequence: instead of going back halfway, it skips steps towards the end. Then I could just use a while loop.
Some other posts on this forum have led me to believe that "continue" is a dangerous tool.
Regards
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 11월 25일
"continue" isn't any more dangerous than "break" in MATLAB. The only thing you need to watch out for is if you are in nested loops and want the "continue" to go right out of the inner loop and to the next iteration of the outer loop.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by