1.Matlab鍵盤鼠標(biāo)輸入學(xué)習(xí)
包括鍵盤監(jiān)聽KbWait KbCheck、鼠標(biāo)監(jiān)聽GetMouse
%反應(yīng)錄入
%Mouse & Keyboard
ListenChar(1);
%設(shè)置0將不能監(jiān)聽鍵盤輸入
%設(shè)置1或者不設(shè)置任何值則可以監(jiān)聽鍵盤輸入
%設(shè)置2可以監(jiān)聽鍵盤輸入但是抑制命令行輸出
%電腦中鍵盤的編碼
edit KbName %kbNameResult = KbName(arg)
KbName('UnifyKeyNames');
space = KbName('space'); %鍵位與數(shù)字一一對(duì)應(yīng)
a = KbName(40);
%使用while監(jiān)聽鍵盤
while(1)
format long;
disp(GetSecs); %使用每個(gè)操作系統(tǒng)上最高精度的實(shí)時(shí)時(shí)鐘。
end
%KbWait KbCheck
help KbWait
%[secs, keyCode, deltaSecs] = KbWait([deviceNumber][, forWhat=0][, untilTime=inf])
%等待直到任何鍵按下,并選擇性地返回時(shí)間(以秒為單位)和鍵盤狀態(tài)的 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對(duì)應(yīng)命令行顯示 2對(duì)應(yīng)命令行不顯示
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
%獲取鼠標(biāo)
help GetMouse
%[x,y,buttons,focus,valuators,valinfo] = GetMouse([windowPtrOrScreenNumber][, mouseDev])
%x y為鼠標(biāo)當(dāng)前向量值 button為鼠標(biāo)上按鈕數(shù)量
figure
x = [];
y = [];
while(1)
[x(end+1),y(end+1),buttons] = GetMouse;%獲取鼠標(biāo)現(xiàn)在位置
plot(x,1000-y); %由于屏幕中位置0在上方 與matlab繪制y軸相反 用1000減去y得到反轉(zhuǎn)值
if(find(buttons==1)) %左鍵退出
break;
end
WaitSecs(0.1); %每隔0.1秒繪制一下
end
如圖為鼠標(biāo)繪制的圖像:
2.PsychToolBox設(shè)置反應(yīng)收集
之前的代碼參考上一篇文章:Psychtoolbox使用Screen時(shí)間控制以幀隨機(jī)播放圖片
在反應(yīng)收集部分添加鍵盤輸入項(xiàng),對(duì)應(yīng)保存結(jié)果。被試者對(duì)圖片刺激進(jìn)行判斷給出反應(yīng)結(jié)果。
被試者反應(yīng)結(jié)果(鍵位、匹配結(jié)果、反應(yīng)時(shí)間)呈現(xiàn)如圖:文章來源:http://www.zghlxwxcb.cn/news/detail-571276.html
代碼:文章來源地址http://www.zghlxwxcb.cn/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);
% 呈現(xiàn)一個(gè)視角5度的圖片
pixs = deg2pix(5,15.5,wsize(3),50); %計(jì)算該條件下對(duì)應(yīng)視角在屏幕中的像素?cái)?shù)
%畫圖
%首先加載圖片
cd Screen_tutorial/images
files = dir('*tif*');
for i = 1:length(files)
Image_pool{i} = imread(files(i).name);
end
%進(jìn)行一一組合一共有多少條件
%設(shè)定SOA條件 SOA為啟動(dòng)刺激(Prime)呈現(xiàn)起到目標(biāo)刺激(Probe)呈現(xiàn)之間的時(shí)間
%可理解為圖片呈現(xiàn)的時(shí)間
%由于1秒、0.5秒這樣表示不能整除每幀0.0061s的時(shí)間,為保證實(shí)驗(yàn)的準(zhǔn)確性,不采用秒的方式來呈現(xiàn)圖片
%更改采用幀數(shù)來展示圖片,這里設(shè)置了一個(gè)存儲(chǔ)時(shí)間的元胞數(shù)組SOA_pool 為了好計(jì)算 間隔專門設(shè)置成每幀所用時(shí)間
SOA_pool = [6.1:6.1:24.4000]/1000;
SOA_pool = time2frame(flipIntv,SOA_pool); %調(diào)用time2frame函數(shù) 該函數(shù)將時(shí)間四舍五入取整求幀數(shù)
Mask_pool = 1:2; %Mask_pool用來表示實(shí)驗(yàn)過程中是否有mask圖片
%計(jì)算共有多少次數(shù) 遍歷所有條件
Total_trial = length(Image_pool)*length(SOA_pool)*length(Mask_pool);
%分配圖像在這么多試次的分布(隨機(jī))
%由于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;
%秒轉(zhuǎn)化為幀
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);
%預(yù)分配內(nèi)存給反應(yīng)矩陣
response_pool = []; %反應(yīng)鍵位
RT_pool = []; %反應(yīng)時(shí)間
response_ture = []; %反應(yīng)正確/錯(cuò)誤
n_trial = 12;%測試使用12個(gè)對(duì)象 Total_trial太多這里不使用
%for i = 1:Total_trial
for i = 1:n_trial
Screen('FillRect',win,128);%背景與注視點(diǎn)
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))
%呈現(xiàn)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
%不呈現(xiàn)Mask
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win,time_stamp+(SOA_pool(SOA_order(i))-0.5)*flipIntv);
end
%呈現(xiàn)指導(dǎo)語,提取設(shè)置好指導(dǎo)語與鍵位對(duì)應(yīng)
Screen('FillRect',win,128);
txt = '2 Animal'; %對(duì)應(yīng)w鍵位
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy,255);
txt = '1 Object'; %對(duì)應(yīng)q鍵位
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy-bRect(4),255);
txt = '3 Scene'; %對(duì)應(yīng)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);
%要求被試反應(yīng)
start_time = GetSecs; %獲取最新時(shí)間
response_pool(i) = nan;
RT_pool(i) = nan;
while(GetSecs-start_time<word_onset*flipIntv) %最多等待時(shí)間
%被試者要在指導(dǎo)語顯示的范圍內(nèi)給出反應(yīng),在這個(gè)范圍內(nèi)獲取鍵位反應(yīng)
[K_down,~,K_code] = KbCheck;
if(K_down)
RT_pool(i) = GetSecs-start_time; %計(jì)算并保存反應(yīng)時(shí)間
%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')) %取當(dāng)前圖片的前三個(gè)字母判斷是否匹配obj
%response true
response_ture(i) = 1;
else
response_ture(i) = 0;
end
elseif(K_code(keyw))
if(strcmp(file_nm(1:3),'ani')) %取當(dāng)前圖片的前三個(gè)字母判斷是否匹配ani
response_ture(i) = 1;
else
response_ture(i) = 0;
end
elseif(K_code(keye))
if(strcmp(file_nm(1:3),'sce')) %取當(dāng)前圖片的前三個(gè)字母判斷是否匹配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
到了這里,關(guān)于Matlab鍵盤鼠標(biāo)輸入學(xué)習(xí)及PsychToolBox對(duì)刺激反應(yīng)結(jié)果的保存的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!