How to convert this code without 'goto' statement?

8 次查看(过去 30 天)
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