Filter löschen
Filter löschen

Counter not working in for loop

1 Ansicht (letzte 30 Tage)
Ranjani Shankar
Ranjani Shankar am 13 Feb. 2019
Kommentiert: Star Strider am 13 Feb. 2019
I have a counter in a for loop like this:
x = 0;
for i = 1:10
x = x+1;
% code that uses the counter
end
However, when I was debugging and removed the semicolon from the x = x+1 statement I get:
x =
0×1 empty double column vector
When I debugged and used disp(i) and disp(x), it would only display the value of i and not x. Does anyone know how to fix this?
  3 Kommentare
Kevin Phung
Kevin Phung am 13 Feb. 2019
it also seems like x is just equivalent to i in every iteration, have you considered just using i?
Ranjani Shankar
Ranjani Shankar am 13 Feb. 2019
the for loop is actually a nested for loop and x is a counter for every iteration of both for loops

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Star Strider
Star Strider am 13 Feb. 2019
We cannot see the ‘code that uses the counter’. Check to see if your code is somehow clearing ‘x’ or setting ‘x’ to [].
  4 Kommentare
Ranjani Shankar
Ranjani Shankar am 13 Feb. 2019
do you have any suggestions for debugging tools other than the ones I've mentioned using?
Star Strider
Star Strider am 13 Feb. 2019
Yes. Specifically: Debug a MATLAB Program (link).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by