MATLAB: Pause program and await keypress

36,192

Solution 1

I'd use the input function:

a = input('Accept this graph (y/n)? ','s')

if strcmpi(a,'y')
    ...
else
    ...
end

Although admittedly it requires two keypresses (y then Enter) rather the one.

Solution 2

Wait for buttonpress opens up a figure, which may be unwanted. Use instead

pause('on');
pause;

which lets the user pause until a key is pressed.

Solution 3

Why not using waitforbuttonpress instead?

Documentation: http://www.mathworks.fr/help/techdoc/ref/waitforbuttonpress.html

Solution 4

You don't want to use waitforbuttonpress since it locks the figure gui (no zooming, panning etc).

pause can cause the command window to steal the focus from the figure.

The solution I find to work best is to open the figure with a null keyPressFcn in order to avoid focus problems:

figure('KeyPressFcn',@(obj,evt) 0);

and then wait for CurrentCharacter property change:

waitfor(gcf,'CurrentCharacter');
curChar=uint8(get(gcf,'CurrentCharacter'));

Solution 5

Wait for key press or mouse-button click:

Example:

w = waitforbuttonpress;
if w == 0
    disp('Button click')
else
    disp('Key press')
end

for more information visit: http://www.mathworks.com/help/matlab/ref/waitforbuttonpress.html

Share:
36,192
CaptainProg
Author by

CaptainProg

Updated on July 09, 2022

Comments

  • CaptainProg
    CaptainProg almost 2 years

    I am writing a program in which at some point a graph is plotted and displayed on screen. The user then needs to press 'y' or 'n' to accept or reject the graph. My current solution uses the PsychToolbox (the actual solution doesn't need to), which includes a command called 'KbCheck' which checks at the time of calling the state of all the keyboard buttons. My code looks like this:

    function [keyPressed] = waitForYesNoKeypress
    keyPressed = 0; % set this to zero until we receive a sensible keypress
    while keyPressed == 0 % hang the system until a response is given
        [ keyIsDown, seconds, keyCode ] = KbCheck; % check for keypress
        if find(keyCode) == 89 | find(keyCode) == 78 % 89 = 'y', 78 = 'n'
            keyPressed = find(keyCode);
        end
    end
    

    The problem is, that the system really does 'hang' until a key is pressed. Ideally, I would be able to scroll, zoom, and generally interact with the graphs that are plotted onscreen so that I can really decide whether or not I want to press 'y' or 'n'!

    I have tried adding 'drawnow;' into the while loop above but that doesn't work: I still am unable to interact with the plotted graphs until after I've accepted or rejected them.

    The solution doesn't have to use PsychToolbox; I assume there are plenty of other options out there?

    Thanks

  • CaptainProg
    CaptainProg over 12 years
    I'd settle for two keypresses! Thanks very much
  • Semjon Mössinger
    Semjon Mössinger over 10 years
    This solution has the advantage that the focus is automatically on the window. So it's not necessary to clic into the Command Window. If you use "pause" this is necessary. The window raised by this function can be closed automatically with the following code: figure(42); waitforbuttonpress; close(figure(42));
  • patrik
    patrik about 10 years
    @Ergodicity Thanks for pointing this out in a comment. I guess clumsy is a bit far to go and removed this from the answer. However, if do not want to open up a figure, for example working with matrices popping up in the command window, I would still go for pause: but in case of wanting to stop a figure stepwise to examine changes, I would say that waitforbuttonpress is a good command.
  • Shai
    Shai about 10 years
    welcome to SO. please post your answer as a comment to user3041840's answer where it belongs.