Currently I am working with a for loop that takes quite some time to run. In order for me to manage my time better I created a simple algortime that predicts when the loop is finished.
tic
t1 = datetime('now');
for i = 1 : 5000
k = toc;
t1 + seconds((k*5000)/i)
end
The only problem is that it is not working, when i = 5000 the time printed should be equal to the current time but it is always current time minus many minutes. I think this is because when my laptop is in lock screen mode the loop is still running but toc is not. is this true? And if yes is there a way to solve this problem.
Thanks,
Oscar

5 件のコメント

Geoff Hayes
Geoff Hayes 2019 年 6 月 11 日
Oscar - can you please clarify this calculation
(k*5000)/i
If k is the elapsed time (in seconds) since the initial call to toc, why do you need to multiply it by 5000 and then divide by the iteration "number"?
Oscar Schyns
Oscar Schyns 2019 年 6 月 11 日
if i = 2500 and k = 1000 seconds it took a 1000 seconds to complete the first 50 % of the script and therefore will probably take 2000 seconds (5000/2500)*k to complete the script. So add 2000 to the time the script started running
Alex Mcaulley
Alex Mcaulley 2019 年 6 月 11 日
The code works for me as you want. At the end, the time is t1 + k seconds.
Oscar Schyns
Oscar Schyns 2019 年 6 月 11 日
Was your computer in lock screen / dark screen mode because I think that is the reason why it is not working?
Alex Mcaulley
Alex Mcaulley 2019 年 6 月 11 日
With the computer in lock screen mode I get the same result.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 6 月 11 日

コメント済み:

2019 年 6 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by