MATLAB:暂停程序并等待按键 [英] MATLAB: Pause program and await keypress

查看:1486
本文介绍了MATLAB:暂停程序并等待按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,在该程序中有时会绘制图形并显示在屏幕上.然后,用户需要按"y"或"n"来接受或拒绝该图.我当前的解决方案使用PsychToolbox(不需要实际的解决方案),其中包括一个名为"KbCheck"的命令,该命令会在调用所有键盘按钮的状态时进行检查.我的代码如下:

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

问题是,系统确实会挂起",直到按下某个键为止.理想情况下,我将能够滚动,缩放并通常与屏幕上绘制的图形进行交互,这样我才能真正决定是否要按"y"或"n"!

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'!

我尝试添加"drawnow;"进入上面的while循环中,但是那行不通:在之后我接受或拒绝了它们之后,我仍然无法与绘制的图表进行交互.

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.

该解决方案不必使用PsychToolbox;我认为那里还有很多其他选择吗?

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

谢谢

推荐答案

我将使用input函数:

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

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

尽管诚然,它需要两次按键(先按Enter再按Enter),而不是一次.

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

这篇关于MATLAB:暂停程序并等待按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆