필터 지우기
필터 지우기

How to terminate the MATLAB code?

조회 수: 3 (최근 30일)
Noor Fatima
Noor Fatima 2022년 9월 23일
댓글: Walter Roberson 2022년 9월 23일
I'm running the following code
n = 10000000;
a = 3.8;
x(1) = 0.5;
tic
for i=1:n
x(i+1)=a*x(i)*(1-x(i));
end
Time = toc
I wanted to set some time limit say Time = 35 s.
How can I apply time limit condition on the above code such that if Time= 35 s. The code will automatically terminate.
  댓글 수: 1
Sharmin Kibria
Sharmin Kibria 2022년 9월 23일
You can move Time=toc inside the loop and break if (Time == 35) .

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

채택된 답변

Davide Masiello
Davide Masiello 2022년 9월 23일
n = 100000000;
a = 3.8;
x(1) = 0.5;
tic
Time = 0;
for i=1:n
if Time > 5
fprintf('You run out of time')
break
end
x(i+1)=a*x(i)*(1-x(i));
Time = toc;
end
You run out of time
disp(Time)
5.0000
  댓글 수: 2
Noor Fatima
Noor Fatima 2022년 9월 23일
@Davide Masiello Thank you,
If there is not a for loop , can we apply time limit ?
e.g.,
tic
X = myfunction(a, n,x)
toc
Walter Roberson
Walter Roberson 2022년 9월 23일
In order to impose a time limit on a calculation without the cooperation of the code, you have to use parfeval() to run the code in a parallel pool or in a background thread pool; you can cancel() the "future" of the worker if it reaches the time limit.
Background pools were introduced relatively recently and do not require additional toolboxes. Parallel pools are older but require Parallel Computing Toolbox

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

추가 답변 (0개)

카테고리

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