How can i write an equivalent GO TO statement in MATLAB

Hello guys I have a Fortran77 Code which I need to write the equivalent code in Matlab. The code is given below.Inside the code there are GO TO statements. I searched all over the net , I know that there is not any 'go to' statement in matlab. Could you please help me with an alternative solution in my code. I will greatly appreciate your help. The Fortran code is given below.
SUBROUTINE HYST0(N,YP,YN,Y,SK1,SK2,UDOT,U,SK,KK,B,ND)
DOUBLE PRECISION YP,YN,Y,SK1,SK2,UDOT,U,SK,B
DIMENSION YP(ND),YN(ND),Y(ND),SK1(ND),SK2(ND),UDOT(ND),U(ND)
DIMENSION B(ND),SK(ND),KK(ND)
DO 100 I=1,N
IF (UDOT(I).GT.0.0D 00) GO TO 50
IF (U(I).GT.YN(I)) GO TO 90
YN(I)=U(I)
YP(I)=YN(I)+0.2D 01*Y(I)
GO TO 80
50 IF (U(I).LT.YP(I)) GO TO 90
YP(I)=U(I)
YN(I)=YP(I)-0.2D 01*Y(I)
80 SK(I)=SK2(I)
KK(I)=-50
GO TO 100
90 SK(I)=SK1(I)
KK(I)=50
100 CONTINUE
RETURN
END

1 Comment

You don't need go to. There are few circumstances in which their use is justifiable. In this case, you could is if else. You need to follow the logic of the program.

Sign in to comment.

Answers (1)

The best solution is to understand the function thoroughly enough to simply rewrite it in Matlab syntax. Alternatively, to transliterate the Fortran to Matlab keeping the looping you just look at each condition and figure out what's going on...just for a beginning, look at the first condition--
DO 100 I=1,N
IF (UDOT(I).GT.0.0D 00) GO TO 50
...
50 CONTINUE
...
100 CONTINUE
is
for i=1:n
if udot(i)<=0
% stuff to 50 here
else
...
It can be a pit(proverbial)appendage when there are breaks across logic areas which may require more extensive refactoring.

5 Comments

Don't you love spaghetti code?
Thank you so much for your answers I tried if else before as you mentioned but it did not solve the problem but I will try again.
You could look for the File Exchange f2matlab converter -- I don't know just what (if anything) it does with GOTO...maybe it gets a start??? (Then again, it may just complain).
dpb
dpb on 25 Jun 2014
Edited: dpb on 25 Jun 2014
As I've said before, I've two basic recommendations on moving Fortran to Matlab --
a) Figure out what it's doing and rewrite in Matlab, or
b) Convert to a mex-file and call from Matlab.
The straight transliteration from one to the other is rarely (if ever) good, particularly with strict F77 or earlier code that lacked the structured constructs. Even tho the subject code isn't really that convoluted, unless one knows the algorithm by it's definition instead of simply reading the code line-by-line, it's difficult at best to ensure one's got an accurate implementation and even if do, the use of the individual loops in Matlab is often a real bottleneck.
well its been a while since i put on my fortran hat but here is a quick cut. If you follow the logic as dpb suggests I did the forloop and the first if statement checks the first if statement of the fortran and the line50 if statement nested. the line 50 if statement does line 90 stuff if true else it does the stuff right after line 50. and so on with the other goto statements.
for I=1:N
if UDOT(I).GT.0.0D 00
if U(I).LT.YP(I)
SK(I)=SK1(I)
KK(I)=50
else
YP(I)=U(I)
YN(I)=YP(I)-0.2D 01*Y(I)
SK(I)=SK2(I)
KK(I)=-50
end
elseif (U(I).GT.YN(I))
SK(I)=SK1(I)
KK(I)=50
else
YN(I)=U(I)
YP(I)=YN(I)+0.2D 01*Y(I)
SK(I)=SK2(I)
KK(I)=-50
end
end

Sign in to comment.

Categories

Asked:

on 25 Jun 2014

Commented:

on 25 Jun 2014

Community Treasure Hunt

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

Start Hunting!