logfilepsychtoolbox

Psychtoolbox- writing out information into a log file


I'm brand new at programming and am attempting some tutorials I've found to familiarize myself with Psychtoolbox. This script presents 20 trials where either an angry or sad face will appear on the left or right side of the screen (both the emotion and location are random). The user then presses either the 's' or 'a' key to respond. This is running fine, but the issue is writing out the information into a log file. For some reason the program crashes whenever I run the script with the current code. Any help anyone could provide would be greatly appreciated! Here is my code:

%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);

Solution

  • The issue with the code you posted is that the following line uses the string variable 'subjectCode' to write the log filename, but this variable hasn't been defined:

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

    You can add for example

    subjectCode = '1';
    

    Anywhere before the call to

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