跳转到内容

MATLAB 编程/Psychtoolbox/示例代码

来自维基教科书,开放的书籍,开放的世界
Back to MATLAB Programming/Psychtoolbox
  • Imageviewer - 在屏幕上显示图像 (OSX)
  • OpenManyScreens - 在所有连接的显示器上打开一个窗口 (OSX)
  • xsearch - 通用视觉搜索代码 (OS9/WIN)
  • ImgShow - 从磁盘加载一系列图像并以随机顺序显示它们指定的时间段 (OS9/WIN)
  • eyelink 工具箱 - 将 sr-research eyelink 视频眼动仪与跨平台 opengl psychtoolbox 一起使用 (OSX/win)

有用脚本

[编辑 | 编辑源代码]

代码片段

[编辑 | 编辑源代码]
  • 一个函数,它接受一个所需键的列表,并返回其中被按下且更靠近传递的列表前端的键的编号。它忽略其他按键。
 function keyPressed = getKeys(keysWanted) % keysWanted is a matrix list of keys you are 
                                           % waiting for e.g, [124 125 kbName('space')]
  FlushEvents('keydown');
  success = 0;
  while success == 0
   pressed = 0;
   while pressed == 0
    [pressed, secs, kbData] = KbCheck;
   end;
    for i = 1:length(keysWanted)
      if kbData(keysWanted(i)) == 1
       success = 1;
       keyPressed = keysWanted(i);
       FlushEvents('keydown');
       break;
      end;
    end;
    FlushEvents('keydown');
   end;


  • 等待一个按键,两秒后继续
  tEnd=GetSecs+2;
  while ~KbCheck & GetSecs<tEnd
    % Sleep one millisecond after each check, so we don't
    % overload the system in Rush or Priority > 0
    WaitSecs(0.001);
  end
  • 将命令窗口置于最前面。即使它不会出现在由 Screen 打开的窗口前面,包含它也是有用的,因为它允许您键入例如 Screen('Closeall') 来关闭已崩溃程序的窗口
commandwindow;
  • 问题: KbCheck 似乎不会等待按键,比预期更早退出循环,并继续返回按键。解决方案: 确保首先等待任何按键释放。例如
commandwindow;
touch=1;
while touch==1
    [touch, secs, keycode] = KbCheck;
    % Sleep one millisecond after each check, so we don't
    % overload the system in Rush or Priority > 0
    WaitSecs(0.001);
end
touch = 0;
i=0;
while ~touch
 % Sleep one millisecond after each check, so we don't
 % overload the system in Rush or Priority > 0
 WaitSecs(0.001);
 [touch, secs, keycode] = KbCheck;
 fprintf('.');
 if i==50
  fprintf('\n');
  i=0;
 end
 i=i+1;
end
clc; % clear command window, removing any typed characters
fprintf('\ntouch down\n\n');
华夏公益教科书