TIme bound for loop
1 次查看(过去 30 天)
显示 更早的评论
Is it possible for me to run a loop for a specified amount of time say 3 seconds?
0 个评论
回答(1 个)
Mark
2013-3-27
This isn't a for loop, but you can get something like what you describe using a while loop.
t = now;
secondsToWait = 3;
while true
if now >= t + secondsToWait/24/60/60
break;
end
% Here you can put what you want to run...
end
2 个评论
Mark
2013-3-27
This isn't perfect because if you have something that takes a long time to run, it won't be stopped by the loop until after it is finished.
Walter Roberson
2013-3-27
Also, if you use "now" then you are going to be referring to clock seconds rather than cpu seconds.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!