goto function

13 views (last 30 days)
nazish
nazish on 23 Nov 2011
Commented: Walter Roberson on 5 Dec 2022
Hi, I need to use goto function in my code, and I know matlab doesn't have a builtin goto function. The file posted on the following link doesn't work for me inside nested loops. http://www.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement Really need help, have a project deadline :(
  2 Comments
Sven
Sven on 23 Nov 2011
In my experience no program *ever* needs to use a goto function. I doubt your assignment required using gotos in MATLAB since it doesn't have them. Perhaps if you included a small sample of your code and what you were *trying* to achieve then someone can help you.
Walter Roberson
Walter Roberson on 23 Nov 2011
Oh, there are programs that *need* goto, to be sure. But they aren't MATLAB programs.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Nov 2011
You will need to rewrite your code so that it does not need any "goto". MATLAB does not support "goto" and probably never will. Certainly you have more chance of winning a lottery than of MathWorks supplying a functional "goto" before your project deadline.
What arrangement of code is it that you have that makes it appear that you need a "goto" ?
If you are just wanting to loop back to earlier code, then enclose that block of code in a "while" statement. "while true" (meaning continue forever unless something interferes) is considered valid programming.
If you are wanting to have a condition in an inner loop "break out" of more than one layer of nested statements, then you can make this work using flags. Example:
for J = 1 : 5000
leave_J = false;
for K = 1 : 10000
if J^5 + K^3*J - 23*K^2 == 0
leave_J = true;
break; %break out of K loop
end
disp('Nothing found yet')
end
if leave_J %test flag
break; %and it was set, so leave J loop
end
end
This might not be as short to program as if you were to "goto" out of the inner loop to beyond the outer loop, but it works quite effectively -- and you can implement it now rather than "Waiting For Gotod"
  2 Comments
Kai
Kai on 5 Dec 2022
Better than using goto statements, would be to refactore the code a little and put the loops into a sub-function. Instead of goto, one can simply use return in this case.
Walter Roberson
Walter Roberson on 5 Dec 2022
However, functions are "heavier weight": you have to have the initialization and cleanup phases.
When I try to quantify the difference, the results are not clear.
Notice here I skip leading entries. In all of the cases I tried, the first few timings (for both possibilities) were much higher, after which they dropped for the rest of the time. That tends to imply time spent in the execution engine optimizing, with it being fully optimized after a few loops. The time spent optimizing is probably different for here in the MATLAB Answers facility than it would be on a desktop where the code was written into files, so I would recommend not relying on the results from this run here.
Notice that both runs had significant peaks, runs in which the times were slower, and that the runs were inconsistent about which was faster otherwise. But if you were to re-run the exact same code, you would encounter other experiments in which the times for one style were nearly always notably higher than for the other... but which of the two is slower can change. And the bursty-ness can change a lot. So the load on the Answers system is making a big difference in the results, and again that means that it would need to be tried on desktop before drawing any conclusions.
The trend seems to be that the version calling an extra function with a return is slower than using a loop, but the variability here on the Answers facility is too much to rely on for a test this size.
A = randi([-9 9], 500, 1);
B = randi([-9 9], 500, 1);
N = 100;
t_inline = do_inline(A, B, N);
t_function = do_function(A, B, N);
skip = 10;
format long g
[mean(t_inline(skip+1:end)), mean(t_function(skip+1:end))]
ans = 1×2
1.0e+00 * 3.36666666666667e-06 3.62222222222222e-06
plot([t_inline(skip+1:end), t_function(skip+1:end)]);
legend({'break inline', 'function return'})
function timings = do_inline(A, B, N)
timings = zeros(N,1);
for K = 1 : N
start = tic;
total = 0;
numrow = size(A,1);
for J = 1 : numrow
total = total + A(J) + B(J);
if total == 0; break; end
end
stop = toc(start);
timings(K) = stop;
end
end
function timings = do_function(A, B, N)
timings = zeros(N,1);
for K = 1 : N
start = tic;
total = add_cols(A, B);
stop = toc(start);
timings(K) = stop;
end
end
function total = add_cols(A,B)
total = 0;
numrow = size(A,1);
for J = 1 : numrow
total = total + A(J) + B(J);
if total == 0; return; end
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!