Psychtoolbox-将信息写到日志文件中

时间:2018-06-20 16:14:32

标签: logfile psychtoolbox

我是编程的新手,我正在尝试一些我发现自己熟悉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 %d\n', nTrials, randomFaceNum,   randomRectNum,response, responseTime, correct);
 fclose(FID);

 nTrials = nTrials + 1;

 end

Screen('Close', window);

1 个答案:

答案 0 :(得分:0)

您发布的代码的问题是,以下行使用字符串变量'subjectCode'写入日志文件名,但尚未定义此变量:

 fileName=strcat(subjectCode,'_log.txt');

您可以添加例如

subjectCode = '1';

致电之前的任何地方

fileName=strcat(subjectCode,'_log.txt');