I’m currently translating code from Fortran to MATLAB manually, and I am unsure of how to translate part of it. (The entire code actually is a 2,000 line subroutine.) The code is below.
C Has series crossed neckline?
120 neckext=x(trough(peaknum-1))+
* dydx*real((t-trough(peaknum-1)))
if(x(t).lt.neckext) goto 130
C NO. Here if series has not crossed neckline, nor new trough found
C Check to see if new trough has been found.
t=t+1
if(t.ge.lastobs) goto 900
if(x(t).lt.min) then
min=x(t)
mindate=t
end if
troughid=min*(1.0+cutoff)
if(x(t).ge.troughid) goto 150
goto 120
C YES. Here if series crossed neckline before new trough found
130 dblcount=0
if(poscount.ge.1) then
DO 132 i=1,poscount
if((enterdt(i)-2.le.t).and.(t.le.enterdt(i)+2)) then
dblcount=dblcount+1
end if
132 continue
if(dblcount.ge.1) then
C write(30,2583) t,Cutnum
2583 format('DoubleCounting episode occurred at ',I5,
* ' with Cutoff = ',F3.1)
goto 150
end if
end if
My problem is with this part of the code:
if(x(t).ge.troughid) goto 150
goto 120
When I was translating this part in MATLAB, I was writing something like:
if x(t,:)>=troughid
t=marker;
minimum=x(t,:);
end
But I don’t know what to do with the label 120. When I translate it, do I write that part again? Because from what I understand, when I go back to 120, the code will be running again. Thanks!
EDIT: As a response to Chris’s question on what labels 150 and 900 do, I’ll post them here.
150 t=marker
min=x(t)
And this is for the label 900.
C Last observation found. This iteration finished.
900 continue
As it should be clear by now, Matlab does not include any variant of a “goto” command. The core Matlab command set appear to be designed around the “structured programming” philosophy. (Which, if I remember my CS ancient history correctly, was the great debate prior to object oriented programming.) Wikipedia has a decent discussion of structured programming.
In the dark days before structured programming, people used to be very excited about flow charts, since that was one of the easiest ways to visualized and understand a piece of code using a lot of
gotostatements (now usually referred to as spaghetti code).I suspect that you will need to flowchart the entire subroutine, and then decide which control flow constructs can best be used to recreate your code. If it is a relatively simple diagram, then you should be able to recreate the entire code with
ifstatements orcasestatements, although a series of small helper functions may be more elegant. If it has a more complex structure, then it may take a little more creativity to translate.