Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No GOTO in MATLAB. Need Help!

1,577 views
Skip to first unread message

Liam

unread,
Nov 11, 2008, 9:26:03 AM11/11/08
to
I know there is not GOTO in MATLAB, so I need some help.

I've been making some old fortran code into matlab langauge and I cant work out how to get round my problem.

I present the code below, it is obvious to see where the GOTO is used. Can anyone help?

for J = IN2F:IN2L;

1

B = H2(J) - H1(K);

if abs(B)<0.1*10^-7;
W2(J) = W1(K);

elseif (B<0) && (K==1) | (B>0) && (K==N1);
W2(J) = 0.0;

elseif B<0;
W2(J)=W1(K-1) + (W1(K)-W1(K-1))*((H2(J)-H1(K-1))./(H1(K)-H1(K-1)));

else
K = K + 1;

GOTO 1

end

end

I've trawelled over the net but not found any sucessful solution yet, so any input would be welcomed.

Thanks in advance

Liam

Rune Allnor

unread,
Nov 11, 2008, 9:58:45 AM11/11/08
to
On 11 Nov, 15:26, "Liam " <lDOTtal...@reading.acDOTuk> wrote:
> I know there is not GOTO in MATLAB, so I need some help.
>
> I've been making some old fortran code into matlab langauge and I cant work out how to get round my problem.

The solution is to find out what the function does and
program it in the language of your choise. One place
to start might be a text on assembler programming, where
one still uses the old GOTO type constructs to implement
stuff like for loops, while lopps and conditional branching.

Once you know the generic patterns you *might* have a
chance to decode code like the one you posted.

Rune

Husam Aldahiyat

unread,
Nov 11, 2008, 10:05:24 AM11/11/08
to
"Liam " <lDOTt...@reading.acDOTuk> wrote in message <gfc4lr$jc2$1...@fred.mathworks.com>...

Use RECURSION via FUNCTION, FOR, IF and BREAK.

David

unread,
Nov 11, 2008, 10:07:01 AM11/11/08
to
for J = IN2F:IN2L;
while(1) % infinite loop

B = H2(J) - H1(K);
if abs(B)<0.1*10^-7;
W2(J) = W1(K);
break;

elseif (B<0) && (K==1) | (B>0) && (K==N1);
W2(J) = 0.0;
break;

elseif B<0;
W2(J)=W1(K-1) + (W1(K)-W1(K-1))*((H2(J)-H1(K-1))./(H1(K)-H1(K-1)));
break;

else
K = K + 1;
end %end of else
end %of while loop
end %of for loop

there are probably better ways if you restructure the if statement and get rid of the infinite loop but this most closely approximates the fortran i think... not tested.

Carlos Adrian Vargas Aguilera

unread,
Nov 11, 2008, 10:11:03 AM11/11/08
to
You should use a counter ans a WHILE loop instead of a FOR loop because of your variable counter.

Carlos

Husam Aldahiyat

unread,
Nov 11, 2008, 10:30:21 AM11/11/08
to
Yeah, I forgot, you don't need to use FOR at all.

Jack

unread,
Nov 11, 2008, 12:46:12 PM11/11/08
to

Replace goto 1 with continue in the matlab version of your function.

Jack Walker

0 new messages