Is there a "go to" command in MATLAB

2 views (last 30 days)
Is there a 'go to' command in MATLAB? I want to increment a variable and then return to the point where the routine begins. Right now, the variable reverts to the value that I started with.
a = 1
x = 3
for i = a:n
y = factorial(x);
disp(y)
end
a = a + 1
When I run the code as written above, the variable 'a' reverts to the original value.

Accepted Answer

Star Strider
Star Strider on 14 Apr 2017
Your ‘a’ value is outside the loop you posted, so it never gets incremented except after the loop. (This assumes there is not an outer loop we don’t know about, likely because you’ve not defined ‘n’ in your posted code.) You’re not incrementing ‘x’ at all, so ‘y’ will never change.
It will be easier to help you if we know what you want to do.

More Answers (1)

John D'Errico
John D'Errico on 14 Apr 2017
Edited: John D'Errico on 14 Apr 2017
I have no idea what you really wanted that code to do.
But essentially always, when someone pleads for a goto statement in MATLAB, it just means they have not learned how to use existing features in MATLAB. Here a while loop might be appropriate, allowing you to return to a point above. Or, you might put some of that code in a function, then just call the function repeatedly in a for (or while) loop.
There is much you can do, with absolutely no need for a goto here.

Categories

Find more on Loops and Conditional Statements 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!