필터 지우기
필터 지우기

Counter not working in for loop

조회 수: 1 (최근 30일)
Ranjani Shankar
Ranjani Shankar 2019년 2월 13일
댓글: Star Strider 2019년 2월 13일
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
Kevin Phung
Kevin Phung 2019년 2월 13일
it also seems like x is just equivalent to i in every iteration, have you considered just using i?
Ranjani Shankar
Ranjani Shankar 2019년 2월 13일
the for loop is actually a nested for loop and x is a counter for every iteration of both for loops

댓글을 달려면 로그인하십시오.

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by