How to convert this code without 'goto' statement?

2 views (last 30 days)
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

Answers (1)

Roger Stafford
Roger Stafford on 4 Nov 2014
Edited: Roger Stafford on 4 Nov 2014
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.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!