2015-03-30 32 views
0

我對Matlab很新,但真的想要改進。對於我的實驗,我想顯示參與者回答是/否的圖片,使用兩個不同的密鑰(f & g),然後顯示下一張圖片,並且重複如此之前。在MatLab中重複試驗

呈現圖片,使用密鑰的作品,但我不能得到它重複審判。因此我的問題是如何讓程序重複/循環我的試驗? 到目前爲止,我的代碼中是否存在錯誤,或者是否有其他代碼需要使用?

這是我到目前爲止的代碼

function try1_6() 

cleanupObj= onCleanup(@() myCleanupFxn); 

% PRETEST 
% Initialize screen with black background 
winID = Screen('openWindow',0, [0 0 0]); 

%Parameter 
backcol=255; 
textcol=0; 

% Load image file(s) 
structimages= []; 
TheImagesdir = dir('theImagesdir/*.jpg'); 
for i=1: length(TheImagesdir); 
    TheImages = imread(['theImagesdir/' TheImagesdir(i).name], 'JPEG'); 

    % Get width and height 
    imageX = size(TheImages,2); 
    imageY = size(TheImages,1); 

    % Convert to texture 
    myTexture = Screen('MakeTexture', winID, TheImages); 

    % Set destination rectangle 
    destRect = [50 100 50+imageX 100+imageY]; 

    %save to structure 
    structimages(end+1).filename=TheImagesdir(i).name; 
    structimages(end).destRect= destRect; 
    structimages(end).texture= myTexture; 
end 

%Make triallist 
numberOfItems= [5]; %list of all possible items 
Nrepeats=4; 
Response=0; 
TrialList=HH_mkTrialList({numberOfItems Response},Nrepeats); 


%PRESENTATION 

for trialnum=1:size(TrialList,1) 
    nitems = TrialList(trialnum,1); 

    Screen('FillRect', winID,backcol); % makes the screen blank 

    %displays text 
    DrawFormattedText(winID,'dkjfghaslkdfglksdjgfh','center','center',textcol); 
    Screen('Flip', winID) 
    HH_waitForKeyPress({'space'}); % waits for spacebar to be pressed 
    Screen('FillRect',winID,backcol); 
    Screen('Flip',winID); 
    WaitSecs(1); 

    %display picture 
    whichTheImages= randi(length(TheImagesdir)); % randomly selects image for directory 
    Screen('FillRect',winID,backcol); 
    Screen('DrawTexture', winID, myTexture, [], destRect); 

    Screen('Flip', winID); 
    HH_waitForKeyPress({'f','j'},5) 

    if resp==-1 
     break 
    end 

    TrialList(trialnum,4)= response; %records response 

end 

end 

function myCleanupFxn() 
    Screen('CloseAll') 
end 
+1

您忘記提及的一個非常重要的事情是,您正在使用[Psychtoolbox](http://psychtoolbox.org/)作爲屏幕內容。 – Setsu 2015-03-30 22:00:35

+0

另外,請正確縮進您的代碼。 'try'和'for'塊的主體應該用製表符縮進。另外,不要把整個事情都包裝在一個巨大的'try'中。 – Setsu 2015-03-30 22:03:30

+0

這不是一個完整的問題。'HH_mkTrialList'和'HH_waitForKeyPress'都是自定義方法,你沒有解釋他們做了什麼。請參閱[this](http://stackoverflow.com/help/how-to-ask)在本網站上發佈問題的指南。 – Setsu 2015-03-30 22:18:28

回答

0

有一些的,你需要先解決你的代碼的問題。首先,在聲明/初始化之前使用TrialListMake triallist代碼塊在for循環的主體中似乎不合適,並且應該放在循環TrialList之前。

你的第二個問題是加載圖像的內部for循環。現在,它會加載每個試用版中的目錄中的所有圖像,即你沒有理由這樣做,你應該把這個for循環放在循環之外。此外,您的原始代碼從未按預期工作,因爲您從不在任何地方保存加載的紋理; myTexture被您文件夾中最後一張圖片覆蓋,這是您獲得的唯一紋理。因此,除了在循環之前預加載圖像之外,還需要將它們保存在數據結構中,以便稍後在試用循環中使用它們。一個簡單的struct將很好的工作在這裏:

structImages = []; 
TheImagesdir = dir('theImagesdir/*.jpg'); 
for i = 1:length(TheImagesdir); 
    TheImages = imread(['theImagesdir/' TheImagesdir(i).name], 'JPEG'); 

    % Get width and height 
    imageX = size(TheImages,2); 
    imageY = size(TheImages,1); 

    % Convert to texture 
    myTexture = Screen('MakeTexture', winID, TheImages); 

    % Set destination rectangle 
    destRect = [50 100 50+imageX 100+imageY]; 

    %save to structure 
    structImages(end+1).filename = TheImagesdir(i).name; 
    structImages(end).destRect = destRect; 
    structImages(end).texture = myTexture; 
end 

有你的代碼中的其他不一致:

  1. whichTheIamges被定義但未使用
  2. resp在比較if resp==-1使用,但沒有定義
  3. response在定義之前保存到TrialList

最後,最大的問題是Screen('CloseAll', winID);是在試用循環裏面,所以你在第一次試用之後拆掉整個演示平臺。僅供參考,如我的評論所述,將您的整個腳本封裝在try區塊中實際上是一種不好的做法。我懷疑你這樣做是因爲你想能夠Ctrl + C中期任務,但有一個更好的方法來做到這一點。如果你使整個腳本成爲一個函數,那麼你可以使用onCleanup方法在你的函數退出時(無論是通常,錯誤還是中斷)執行代碼。方法如下:

function myScript() 
%//make your script a function. There is an additional advantages to doing this: 
%//function performance is better than script performance. 

%//blah-blah-blah 

%//setup the cleanup object before opening screen 
cleanupObj = onCleanup(@() myCleanupFxn); 

%//open the screen 
winID = Screen('openWindow',0, [0 0 0]); 

%//blah-blah-blah 

end 

function myCleanupFxn() 
    %//local function, not visible outside of this file 
    Screen('CloseAll'); 
end 
+0

非常感謝你的回答,真的。我收到了你提出的大部分意見,併爲我澄清了問題。我試圖盡我所能地應用你的評論,但似乎沒有成功。 我似乎不包括評論中的代碼(我也是這個網站的新手),所以在我原來的問題中改變了它。 – 2015-03-31 22:06:36

+0

@MDruyvesteyn請查看提問問題的操作指南。每個問題都應該有一些具體的答案,而不僅僅是「我的代碼不起作用」。 – Setsu 2015-03-31 22:39:15

+0

oke,生病再試一次:我如何讓程序重複/循環我的試驗?最終我想要一張圖片+按鍵,多次重複此操作並記錄按鍵響應 – 2015-03-31 23:49:36