Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break loop after designated length of time in Matlab

Tags:

timer

matlab

I'm a bit confused by the tic function, but I'm not sure if there's something 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 calls to tic will clobber the original time. Is there a way to access the current value of tic without overwriting it?

like image 370
chimeracoder Avatar asked Feb 09 '12 17:02

chimeracoder


People also ask

How do you break a for loop in MATLAB?

The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement. break is not defined outside a for or while loop. To exit a function, use return .

How do you break an infinite loop in MATLAB?

when an loop is running ctrl + c (just ctrl and c ) will exit any loop..

How many loops does break break MATLAB?

This functionality is not availble when using the function BREAK. BREAK will only break out of the loop in which it was called. As a workaround, you can use a flag variable along with BREAK to break out of nested loops.

How do you do a break in MATLAB?

Alternatively, you can press the F12 key to set a breakpoint at the current line. If you attempt to set a breakpoint at a line that is not executable, such as a comment or a blank line, MATLAB sets it at the next executable line.


1 Answers

The function NOW returns a serial date number (i.e. an encoded date and time). You should instead be pairing the call to TIC with a call to TOC to perform stopwatch-like timing, like so:

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 could simplify your loop like so:

while (toc(timerID) < RUNTIME)

    %# Perform some process

end
like image 121
gnovice Avatar answered Nov 15 '22 05:11

gnovice