infinite loop that terminates with keyboard press in matlab

You can create an infinite loop in MATLAB using the while statement and use the waitforbuttonpress function to terminate the loop upon a keyboard press. Here's an example:

main.m
while true
    % your code to be executed infinitely
    pause(0.1); % optional delay to reduce CPU usage
    if waitforbuttonpress
        break; % break the loop on keyboard press
    end
end
194 chars
8 lines

In this code, the while true statement creates an infinite loop that executes your code (which you should replace with your own code). The waitforbuttonpress function waits for a keyboard or mouse button press, and returns 1 if a keyboard key was pressed. If the returned value is 1, the break statement terminates the loop. The pause function with an optional delay is used to reduce CPU usage.

gistlibby LogSnag