我是编程新手,正在尝试一些我发现的教程来熟悉Psychtoolbox。这个脚本展示了20个试验,其中愤怒或悲伤的面孔将出现在屏幕的左侧或右侧(情绪和位置都是随机的(。然后,用户按"s"或"a"键进行响应。这运行良好,但问题是将信息写出到日志文件中。由于某种原因,每当我使用当前代码运行脚本时,程序都会崩溃。任何人可以提供的任何帮助将不胜感激!这是我的代码:
%Clear the screen
clear all;
%Open the on screen window
[window, rect] = Screen('OpenWindow', 0);
%Find the center of the screen
[xCenter, yCenter] = RectCenter(rect);
%Create sad picture textures
sadFaceData = imread('sadface.jpg');
[imageHeight, imageWidth, colorChannels] = size(sadFaceData);
sadFaceTexture = Screen('MakeTexture', window, sadFaceData);
%Create angry face textures
angryFaceData = imread('angryface.jpg');
[imageHeight, imageWidth, colorChannels] = size(angryFaceData);
angryFaceTexture = Screen('MakeTexture', window, angryFaceData);
%Define coordinates of the images
distanceFromCenter = 100;
leftRect = [xCenter-distanceFromCenter-imageWidth, yCenter-imageHeight/2, xCenter-distanceFromCenter, yCenter+imageHeight/2];
rightRect= [xCenter+distanceFromCenter, yCenter-imageHeight/2, xCenter+distanceFromCenter+imageWidth, yCenter+imageHeight/2];
%Set up responses
sad = KbName('s');
angry = KbName('a');
%Get the number of total trials and sad face trials
nTrials = 0;
correct = 0;
totalcorrect = 0;
%Loop for the number of trials
while nTrials < 20
%Random number of 1 or 2
randomFaceNum = randi(2);
randomRectNum = randi(2);
%Loop determining face and location
if randomFaceNum == 1 && randomRectNum == 1
Screen('DrawTexture', window, sadFaceTexture, [], leftRect);
elseif randomFaceNum == 1 && randomRectNum == 2
Screen('DrawTexture', window, sadFaceTexture, [], rightRect);
elseif randomFaceNum == 2 && randomRectNum == 1
Screen('DrawTexture', window, angryFaceTexture, [], leftRect);
else
Screen('DrawTexture', window, angryFaceTexture, [], rightRect);
end
[stimOnset] = Screen('Flip', window);
%Wait for the key press
[secs, keyCode] = KbStrokeWait;
response=KbName(keyCode);
responseTime = secs - stimOnset;
%Determine if the response was correct
if randomFaceNum == 1
if response == 's'
correct = 1;
end
else
if response == 'a'
correct = 1;
end
end
totalcorrect = totalcorrect + correct;
%Set up logfile where data will print to
fileName=strcat(subjectCode,'_log.txt');
FID=fopen(fileName, 'a');
fprintf(FID,'%d %d %d %s %.0f %dn', nTrials, randomFaceNum, randomRectNum,response, responseTime, correct);
fclose(FID);
nTrials = nTrials + 1;
end
Screen('Close', window);
您发布的代码的问题在于,以下行使用字符串变量"subjectCode"来写入日志文件名,但尚未定义此变量:
fileName=strcat(subjectCode,'_log.txt');
例如,您可以添加
subjectCode = '1';
呼叫前的任何地方
fileName=strcat(subjectCode,'_log.txt');