0

我对 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
4

1 回答 1

0

您的代码存在许多需要解决的问题。首先,TrialList在声明/初始化之前使用。代码块在循环Make 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. responseTrialList在定义之前保存到

最后,最大的问题是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
于 2015-03-30T23:43:59.100 回答