how to stop simulation when enter in a infinite loop

hi
how i deal in a situation where by mistake infinite loop initiate or where i want to see step by step response of any program or loop. kindly help me
regards

댓글 수: 1

B.k Sumedha
B.k Sumedha 2015년 7월 4일
편집: B.k Sumedha 2015년 7월 4일
U can use debug option or else use breakpoints.

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

 채택된 답변

Geoff Hayes
Geoff Hayes 2015년 7월 4일
Mudasir - sometimes, if I have written some code that makes use of a while loop, I will include a maximum iteration counter to prevent the code from getting stuck in that loop. For example, if the while loop looks something like
while someCondition
% do something
end
then I would change this to
MAX_ITERATIONS = 1000;
iterCount = 0;
while someCondition && iterCount <= MAX_ITERATIONS
% do something
iterCount = iterCount + 1;
end
if iterCount > MAX_ITERATIONS
fprintf('exited loop perhaps because max iteration count reached\n');
end

댓글 수: 5

thanks sir :)
Using a failsafe like Geoff suggested is an excellent idea . I always do it and in fact, consider it an essential idea. I suggest you permanently adopt this essential programming concept. With my code, which needs to be super robust and validated, about half of the lines of code are taken up with comments, fail-safes, input validations, error checking, error logging, bulletproofing/foolproofing/idiotproofing, try/catch, attempted repair of bad inputs (workarounds), etc.
Mudasir Ahmed
Mudasir Ahmed 2015년 7월 4일
편집: Mudasir Ahmed 2015년 7월 4일
sir as i apply above logic , following error occurs
while fitness(:,1)>2 && iterCount <= MAX_ITERATIONS
"Operands to the and && operators must be convertible to logical scalar values.""
Mudsair - fitness must have more than one row. Note that fitness(:,1) will return a column vector of ones and zeros and so it is this column that is causing a problem with the conditional operator. What does the condition
fitness(:,1) > 2
mean to you? Are you checking to see if all elements in the first column are greater than 2? Or, is this condition considered true (by you) if at least one element in the first column is greater than 2?
If the former, then use all as
all(fitness(:,1)>2)
to return a single logical value (0 or 1). If the latter, then use any to determine if at least one value in your column is non-zero (i.e. one) as
any(fitness(:,1)>2)
Try the option that best suits your needs.
perfect answer sir :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2015년 7월 4일

댓글:

2015년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by