在MatLab中重复试验

时间:2015-03-30 21:35:45

标签: matlab repeat trial psychtoolbox experimental-design

我对Matlab很新,但真的想要改进。对于我的实验,我想要显示一个参与者回答是/否的图片,使用两个不同的按键(f& g),然后显示下一张图片,然后重复此过程。

呈现图片,使用键可以使用,但我无法重复试用。因此,我的问题是如何让程序重复/循环我的试用? 到目前为止我的代码中是否有问题,或者我应该使用其他编码吗?

到目前为止这是我的代码

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 个答案:

答案 0 :(得分: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
  4. 最后,最大的问题是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