Counter not working in for loop
Mostrar comentarios más antiguos
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 comentarios
Mark Sherstan
el 13 de Feb. de 2019
Do you have a clear workspace? I dont see anything wrong with that statment and it works fine on my machine.
Kevin Phung
el 13 de Feb. de 2019
it also seems like x is just equivalent to i in every iteration, have you considered just using i?
Ranjani Shankar
el 13 de Feb. de 2019
Respuestas (1)
Star Strider
el 13 de Feb. de 2019
0 votos
We cannot see the ‘code that uses the counter’. Check to see if your code is somehow clearing ‘x’ or setting ‘x’ to [].
4 comentarios
Ranjani Shankar
el 13 de Feb. de 2019
Star Strider
el 13 de Feb. de 2019
‘So it shouldn't be clearing x or setting it to [].’
It is obvious that something is clearing it. I suggest that you use the debugging tools to see what ‘x’ is doing, and where.
Ranjani Shankar
el 13 de Feb. de 2019
Star Strider
el 13 de Feb. de 2019
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!