필터 지우기
필터 지우기

while loop keeps running forever

조회 수: 6 (최근 30일)
Jonathan Ashley
Jonathan Ashley 2022년 12월 2일
댓글: Zahrah Walid 2022년 12월 3일
I'm having trouble with my while loops. when I run the program it keeps going forever. I dont know if why. Can anyone help?
NCJT=2
NSC=zeros(NCJT*NJ,1)
B=1
C=0
D=NDOF
NS=size(MSUP,1)
while B:NJ
Count=0
B1=1
while B1:NS
while ( (MSUP(B1,1)=B) )
Count=1
B2=1
while ( (MSUP(B1,B2+1)=1) )
B3=(B-1)*NCJT+B2
while ( (B2:NCJT) )
D=D+1
NSC(B3)=D
B2=B2+1
endwhile
endwhile
C=C+1
NSC(B3)=C
endwhile
B1=B1+1
endwhile
while ( (Count=0) )
B2=1
while B2:NCJT
B3=(B-1)*NCJT+B2
C=C+1
NSC(B3)=C
B2=B2+1
endwhile
endwhile
B=B+1
endwhile
  댓글 수: 2
Zahrah Walid
Zahrah Walid 2022년 12월 2일
Take care that closing the while loop is by "end" not "endwhile".
Jonathan Ashley
Jonathan Ashley 2022년 12월 2일
it still goes infinite after changing it says repeatedly B3=3

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

채택된 답변

Zahrah Walid
Zahrah Walid 2022년 12월 2일
편집: Zahrah Walid 2022년 12월 2일
First, it's "end" not "endwhile" for closing the loop.
Second, the condition must be logical value; in other words, you should compare using "==" insteade of "=" in line 11 and 13 and edit the others to be valid conditions (logical values).
  댓글 수: 2
Jonathan Ashley
Jonathan Ashley 2022년 12월 2일
thank you, Zahrah this is the mistake i was having me being new to matlab coding i didnt know to use "==" this was super helpful
Zahrah Walid
Zahrah Walid 2022년 12월 3일
You are welcome, Jonathan.
As you are new to MATLAB, I highly recommend you taking MATLAB Onramp course which will give you a suitable base of knowledge to start from. It's free and about 2-hour long only.
Regarding your code, please illustrate its function to debug it.

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

추가 답변 (1개)

John D'Errico
John D'Errico 2022년 12월 2일
편집: John D'Errico 2022년 12월 2일
I think you seriously need to spend some time with the MATLAB tutorials.
  1. while is not terminated by endwhile
  2. A while loop is not written as: while B:NJ. You probably want to learn what a for loop does instead.
  3. You perform a test using ==, NOT =. the single = performs an assignment, which is not legal inline like that.
There must be many other problems, since the code you have not written is invalid MATLAB syntax in many places. This code will not run forever. In fact, it will fail to run immediately in MATLAB.
  댓글 수: 1
Jonathan Ashley
Jonathan Ashley 2022년 12월 2일
well sorry to say John this did work in matlab and since theres no other B# in my code throughtout the whole. I think your response doesn't help me very much.

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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by