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

chi*_*der 6 matlab timer

我对tic函数感到有点困惑,但我不确定是否有更好的东西可以用来做我想做的事情.在psuedo-Matlab中:

startTime = tic

while(true)

   #some_stochastic_process

   if(now - startTime > RUNTIME)
     break;
   end
end
Run Code Online (Sandbox Code Playgroud)

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

gno*_*ice 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
Run Code Online (Sandbox Code Playgroud)

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

while (toc(timerID) < RUNTIME)

    %# Perform some process

end
Run Code Online (Sandbox Code Playgroud)