How to convert this code without 'goto' statement?

조회 수: 3 (최근 30일)
Aswathy
Aswathy 2014년 11월 4일
편집: Roger Stafford 2014년 11월 4일
Since goto statement is not valid in matlab, how can I use the following code written in embedded matlab function:
function y = fcn(clk, lfsr)
jump :
upperT=1000;
count=1;
T=666;
y=1;
label :
if clk==1
Ton=0.806*T;
if Ton<T
y=1;
if count<Ton
count=count+1;
goto label;
elseif Ton<count<T
y=0;
count=count+1;
goto label;
elseif count>T
count=1;
T=lfsr+upperT;
goto label;
else
goto jump;
end
else
goto jump;
end
else
goto jump;
end
end

답변 (1개)

Roger Stafford
Roger Stafford 2014년 11월 4일
편집: Roger Stafford 2014년 11월 4일
That is code you wouldn't want to use. As it stands the function has no way to exit. Regardless of the results of all the 'if', 'elseif' and 'else' conditions, it would always jump back to either "label" or "jump".
Be assured that valid code (which this isn't) can always be transformed so that it would avoid having to do a 'goto', though in some cases it might become somewhat more complicated. Actually this code can be revised so as to avoid goto's but I prefer not to waste time on it because of its erroneous nature.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by