1.Matlab键盘鼠标输入学习
包括键盘监听KbWait KbCheck、鼠标监听GetMouse
%反应录入
%Mouse & Keyboard
ListenChar(1);
%设置0将不能监听键盘输入
%设置1或者不设置任何值则可以监听键盘输入
%设置2可以监听键盘输入但是抑制命令行输出
%电脑中键盘的编码
edit KbName %kbNameResult = KbName(arg)
KbName('UnifyKeyNames');
space = KbName('space'); %键位与数字一一对应
a = KbName(40);
%使用while监听键盘
while(1)
format long;
disp(GetSecs); %使用每个操作系统上最高精度的实时时钟。
end
%KbWait KbCheck
help KbWait
%[secs, keyCode, deltaSecs] = KbWait([deviceNumber][, forWhat=0][, untilTime=inf])
%等待直到任何键按下,并选择性地返回时间(以秒为单位)和键盘状态的 keyCode向量
a = GetSecs;
[secs,keyCode,deltaSecs] = KbWait();
%显示输入的键位
KbName(find(keyCode==1));
help KbCheck
KbCheck
[keylsDown,secs,keyCode,deltaSecs] = KbCheck;
% keyIsDown 1 if any key, including modifiers such as <shift>,
% <control> or <caps lock> is down.
% secs Time of keypress as returned by GetSecs.
% keyCode A 256-element logical array. Each bit
% within the logical array represents one keyboard key.
% If a key is pressed, its bit is set, othewise the bit
% is clear. To convert a keyCode to a vector of key
% numbers use FIND(keyCode). To find a key's keyNumber
% use KbName or KbDemo
ListenChar(1); %1对应命令行显示 2对应命令行不显示
while(1)
[K_down,secs,K_code] = KbCheck;
if(K_down)
disp(KbName(find(K_code==1)));
if(K_code(space))
break;
end
end
end
%获取鼠标
help GetMouse
%[x,y,buttons,focus,valuators,valinfo] = GetMouse([windowPtrOrScreenNumber][, mouseDev])
%x y为鼠标当前向量值 button为鼠标上按钮数量
figure
x = [];
y = [];
while(1)
[x(end+1),y(end+1),buttons] = GetMouse;%获取鼠标现在位置
plot(x,1000-y); %由于屏幕中位置0在上方 与matlab绘制y轴相反 用1000减去y得到反转值
if(find(buttons==1)) %左键退出
break;
end
WaitSecs(0.1); %每隔0.1秒绘制一下
end
如图为鼠标绘制的图像:
2.PsychToolBox设置反应收集
之前的代码参考上一篇文章:Psychtoolbox使用Screen时间控制以帧随机播放图片
在反应收集部分添加键盘输入项,对应保存结果。被试者对图片刺激进行判断给出反应结果。
被试者反应结果(键位、匹配结果、反应时间)呈现如图:
文章来源:https://www.toymoban.com/news/detail-571276.html
代码:文章来源地址https://www.toymoban.com/news/detail-571276.html
global screens screenNumber win wsize flipIntv cx cy time_stamp
try
ListenChar(1);
KbName('UnifyKeyNames');
space = KbName('space');
keyq = KbName('q');
keyw = KbName('w');
keye = KbName('e');
HideCursor;
InitializeMatlabOpenGL;
Screen('Preference','SkipSyncTests',1);
screens = Screen('Screens');
screenNumber = max(screens);
[win,wsize] = Screen('OpenWindow',screenNumber);
cx = wsize(3)/2;
cy = wsize(4)/2;
flipIntv = Screen('GetFlipInterval',win); %我电脑屏幕刷新率为165Hz 1/165 = 0.0061 即flipIntv的值为0.0061
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win);
% 呈现一个视角5度的图片
pixs = deg2pix(5,15.5,wsize(3),50); %计算该条件下对应视角在屏幕中的像素数
%画图
%首先加载图片
cd Screen_tutorial/images
files = dir('*tif*');
for i = 1:length(files)
Image_pool{i} = imread(files(i).name);
end
%进行一一组合一共有多少条件
%设定SOA条件 SOA为启动刺激(Prime)呈现起到目标刺激(Probe)呈现之间的时间
%可理解为图片呈现的时间
%由于1秒、0.5秒这样表示不能整除每帧0.0061s的时间,为保证实验的准确性,不采用秒的方式来呈现图片
%更改采用帧数来展示图片,这里设置了一个存储时间的元胞数组SOA_pool 为了好计算 间隔专门设置成每帧所用时间
SOA_pool = [6.1:6.1:24.4000]/1000;
SOA_pool = time2frame(flipIntv,SOA_pool); %调用time2frame函数 该函数将时间四舍五入取整求帧数
Mask_pool = 1:2; %Mask_pool用来表示实验过程中是否有mask图片
%计算共有多少次数 遍历所有条件
Total_trial = length(Image_pool)*length(SOA_pool)*length(Mask_pool);
%分配图像在这么多试次的分布(随机)
%由于matlab从1开始 取模后要加1
Image_order = mod(randperm(Total_trial),length(Image_pool))+1;
SOA_order = mod(randperm(Total_trial),length(SOA_pool))+1;
Mask_order = randi(2,[1,Total_trial])-1;
%秒转化为帧
trial_interval = 1; %试次之间的间隔,单位是秒
trial_interval = time2frame(flipIntv,trial_interval);
fix_onset = 0.5;
fix_onset = time2frame(flipIntv,fix_onset);
mask_onset = 0.5;
mask_onset = time2frame(flipIntv,mask_onset);
word_onset = 3;
word_onset = time2frame(flipIntv,word_onset);
%预分配内存给反应矩阵
response_pool = []; %反应键位
RT_pool = []; %反应时间
response_ture = []; %反应正确/错误
n_trial = 12;%测试使用12个对象 Total_trial太多这里不使用
%for i = 1:Total_trial
for i = 1:n_trial
Screen('FillRect',win,128);%背景与注视点
Screen('FillOval',win,255, [cx-5,cy-5,cx+5,cy+5]);
time_stamp = Screen('Flip',win,time_stamp+(trial_interval-0.5)*flipIntv);
%展示图片
Screen('FillRect',win,128);
Image_Index = Screen('MakeTexture',win,Image_pool{Image_order(i)});
Screen('DrawTexture',win,Image_Index,[],[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
time_stamp = Screen('Flip',win,time_stamp+(fix_onset-0.5)*flipIntv);
if(Mask_order(i))
%呈现mask
Screen('FillRect',win,128);
masks = im2uint8(rand(50));
Image_Index = Screen('MakeTexture',win,masks);
Screen('DrawTexture',win,Image_Index,[],[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
time_stamp = Screen('Flip',win,time_stamp+(SOA_pool(SOA_order(i))-0.5)*flipIntv);
else
%不呈现Mask
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win,time_stamp+(SOA_pool(SOA_order(i))-0.5)*flipIntv);
end
%呈现指导语,提取设置好指导语与键位对应
Screen('FillRect',win,128);
txt = '2 Animal'; %对应w键位
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy,255);
txt = '1 Object'; %对应q键位
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy-bRect(4),255);
txt = '3 Scene'; %对应e键位
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy+bRect(4),255);
time_stamp = Screen('Flip',win,time_stamp+(mask_onset-0.5)*flipIntv);
%要求被试反应
start_time = GetSecs; %获取最新时间
response_pool(i) = nan;
RT_pool(i) = nan;
while(GetSecs-start_time<word_onset*flipIntv) %最多等待时间
%被试者要在指导语显示的范围内给出反应,在这个范围内获取键位反应
[K_down,~,K_code] = KbCheck;
if(K_down)
RT_pool(i) = GetSecs-start_time; %计算并保存反应时间
%Response True?
file_nm = (files(Image_order(i)).name);
response_pool(i) = find(K_code==1); %保存键位
if(K_code(space))%space is [out]
sca;
stats.subinfo={'WZY','Male','21yrs'};
stats.trial_num=Total_trial;
stats.Image_order=Image_order;
stats.image_shown=Image_pool;
stats.SOAs=SOA_pool(SOA_order);
stats.Masks=Mask_pool(Mask_order+1);
stats.response={RT_pool,response_true,response_pool};
stats.docs='Masks = 1 is unmask,Masks = 2 is masked';
save mask_result.mat stats
return
elseif(K_code(keyq))
if(strcmp(file_nm(1:3),'obj')) %取当前图片的前三个字母判断是否匹配obj
%response true
response_ture(i) = 1;
else
response_ture(i) = 0;
end
elseif(K_code(keyw))
if(strcmp(file_nm(1:3),'ani')) %取当前图片的前三个字母判断是否匹配ani
response_ture(i) = 1;
else
response_ture(i) = 0;
end
elseif(K_code(keye))
if(strcmp(file_nm(1:3),'sce')) %取当前图片的前三个字母判断是否匹配sce
response_ture(i) = 1;
else
response_ture(i) = 0;
end
else
response_ture(i) = 0;
end
break
end
end
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win,time_stamp+(word_onset-0.5)*flipIntv);
end
time_stamp = Screen('Flip',win,time_stamp+1);
stats.subinfo={'WZY','Male','21yrs'}; % cell记录
stats.trial_num=Total_trial;
stats.Image_order=Image_order;
stats.image_shown=Image_pool;
stats.SOAs=SOA_pool(SOA_order);
stats.Masks=Mask_pool(Mask_order+1);
stats.response={RT_pool,response_ture,response_pool};
stats.docs='Masks = 1 is unmask,Masks = 2 is masked';
save mask_result.mat stats
Screen('CloseAll');
ListenChar(0);
catch
ListenChar(0);
sca;
end
function pixs=deg2pix(degree,inch,pwidth,vdist)
screenWidth = inch*2.54/sqrt(1+9/16);
pix = screenWidth/pwidth;
pixs = round(2*tan((degree/2)*pi/180)*vdist/pix);
end
function nframe = time2frame(flipIntv,duration)
nframe = round(duration/flipIntv);
end
到了这里,关于Matlab键盘鼠标输入学习及PsychToolBox对刺激反应结果的保存的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!