在Matlab中指定的时间长度后断开循环

时间:2012-02-09 17:40:32

标签: matlab timer

我对抽动功能感到有点困惑,但我不确定是否有更好的东西可供我尝试做的事情。在psuedo-Matlab中:

startTime = tic

while(true)

   #some_stochastic_process

   if(now - startTime > RUNTIME)
     break;
   end
end

但随后对tic的召唤将破坏原始时间。有没有办法在不覆盖的情况下访问tic的当前值?

1 个答案:

答案 0 :(得分:10)

函数NOW返回一个序列日期编号(即编码的日期和时间)。您应该将对TIC的呼叫与TOC的呼叫配对,以执行类似秒表的计时,如下所示:

timerID = tic;  %# Start a clock and return the timer ID

while true

    %# Perform some process

    if(toc(timerID) > RUNTIME)  %# Get the elapsed time for the timer
        break;
    end

end

或者,您可以像这样简化循环:

while (toc(timerID) < RUNTIME)

    %# Perform some process

end