Variable input to MATLAB function resets involuntarily inside while loop. How can I prevent it?
이전 댓글 표시
I have written a function which takes in an integer (int8) as one of the inputs (called iscool). The function runs a while loop and I insert an if-check inside it to break out of the loop. The if-check checks the value of the iscool variable as well and sets the value of imarch to 0 to get out of loop. So basically, the code is something like this.
% Code_snippet
while (imarch == 1)
<some procedures not modifying iscool>
if ((iscool == 0) && (<other condition 1>) && (<other condition 2>))
imarch = 0;
elseif ((iscool == 1) && (<other condition 3>) && (<other condition 4>))
imarch = 0;
end
disp (strcat('Cooling index is: ',num2str(iscool)));
end
The output of the disp command in the first while-loop execution is 0 (which is the input), but it changes to 1 in the subsequent iteration and stays so after that. I have tried removing the if-elseif-end check and the value of iscool stays intact in that case, but I need to have the check in order to be able to get out of the loop. Any sort of help, particularly an insight into why the value might be changing would be great help. Thanks.
답변 (2개)
John D'Errico
2015년 3월 29일
0 개 추천
So you have not provided us all of your code (which would probably be quite lengthy.) Just a shortened version that does what you think it should do. And then you are asking us why your actual code does something that you consider strange. How can we possibly know?
The only solution is for you to learn how to use the debugger. Step through the code line by line if necessary. Or, you can use dbstop to test for a certain event to happen and stop when it did, like a variable changing state. Then look to see what caused that to happen.
Image Analyst
2015년 3월 29일
0 개 추천
Why do both the "if" and the "elseif" set imarch to 0? Should one of them set it to 1?
Under what circumstances to you ever set imarch to 1 so that you can leave the loop?
댓글 수: 2
Pratyaksh Karan
2015년 3월 30일
Image Analyst
2015년 3월 30일
OK, then it should work. So now read John's answer again. We can't fix code that is not broken (like the code you posted). And we can't fix code that we cannot even see (like your actual code that you would not post).
카테고리
도움말 센터 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!