Cycle break after a certain period of time in Matlab

I'm a little confused by the tic function, but I'm not sure if there is anything better for what I'm trying to do. In psuedo-matlab:

startTime = tic while(true) #some_stochastic_process if(now - startTime > RUNTIME) break; end end 

But subsequent tic calls will knock down the original time. Is there a way to access the current tic value without overwriting it?

+6
source share
1 answer

The NOW function returns the serial number (that is, the date and time of encoding). Instead, you must connect the TIC call to the TOC call to perform the stopwatch function โ€” for example, like this:

 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 

Alternatively, you can simplify your loop as follows:

 while (toc(timerID) < RUNTIME) %# Perform some process end 
+10
source

Source: https://habr.com/ru/post/908066/


All Articles