Matlab

From REALab Wiki
Jump to navigation Jump to search

Here is the new matlab script page!

Framework

Loops

Screens

Show / hide mouse cursor

Example: using loops to control blocks and trials

Display

Positioning

Show text onscreen

Loading images into MATLAB

Making Textures

Showing images on screen

Screen Flip

Example: displaying onscreen images

Input / Output

Wait for user input

Check input value

Saving out data

Example: save data to a text file

Psychology Experiment Specific

Subject number

Assigning conditions

Flipping conditions

Condition based image loading

Condition based image display

Display fixation point

Randomizing image order/positioning

Measuring reaction time

Check for correct response

Eyelink

under construction...

Full Examples

Shape Search

Automatic coding of correct trials

The following code will take a participant's data file, and based on their response on each trial, determine whether the answer was correct or incorrect and output a new file with the automated coding included

%INSTRUCTIONS: Make sure you have the participants data file in the same
%folder as this .m file. Data file should follow this naming format:
%'sXX.txt', where XX is the participant's subject number. Run file, enter
%subject number and condition number. Console will show you brief summary
%of accuracy data. Coded data file is saved to the same folder under the
%name 'tsmXX.txt'

%clear existing variables
clear all
clc 

%set subject and condition number
sub_num = input('Enter the subject number: ');
cond_num = input('Enter the condition number: ');

%coding participant responses
filename = strcat('s',int2str(sub_num),'.txt');
data = importdata(filename);

switch cond_num
   case 1
       cantL = 1;
       cantU = 35;
       notL = 36;
       notU = 70;
       mineL = 71;
       mineU = 105;
       newL = 106;
       newU = 210;
   case 2
       cantL = 36;
       cantU = 70;
       notL = 71;
       notU = 105;
       mineL = 1;
       mineU = 35;
       newL = 106;
       newU = 210;
   case 3
       cantL = 71;
       cantU = 105;
       notL = 1;
       notU = 35;
       mineL = 36;
       mineU = 70;
       newL = 106;
       newU = 210;
   case 4
       cantL = 106;
       cantU = 140;
       notL = 141;
       notU = 175;
       mineL = 176;
       mineU = 210;
       newL = 1;
       newU = 105;
   case 5
       cantL = 141;
       cantU = 175;
       notL = 176;
       notU = 210;
       mineL = 106;
       mineU = 140;
       newL = 1;
       newU = 105;
   case 6
       cantL = 176;
       cantU = 210;
       notL = 106;
       notU = 140;
       mineL = 141;
       mineU = 175;
       newL = 1;
       newU = 105;
end

%correct recognition? if old object 123 are correct, if new object 4 right

for i = 1:210
   stimulus = data(i,2);
   if stimulus >= cantL && stimulus <= cantU
       correctResponse = 3; %cant have
       old = 1;
   elseif stimulus >= notL && stimulus <= notU
       correctResponse = 2; %not mine
       old = 1;
   elseif stimulus >= mineL && stimulus <= mineU
       correctResponse = 1; %mine
       old = 1;
   elseif stimulus >= newL && stimulus <= newU
       correctResponse = 4; %new
       old = 0;
   end
   
   %correct sort? does their response match the exact number in list?
   response = data(i,3);
   if response == correctResponse %sort
       data(i,5) = 1;
   else
       data(i,5) = 0;
   end
   
   if old == 1 %recognition
       switch data(i,3)
           case 1
               data(i,6) = 1;
           case 2
               data(i,6) = 1;
           case 3
               data(i,6) = 1;
           case 4
               data(i,6) = 0;
       end
   elseif old == 0
       switch data(i,3)
           case 1
               data(i,6) = 0;
           case 2
               data(i,6) = 0;
           case 3
               data(i,6) = 0;
           case 4
               data(i,6) = 1;
       end
   end
end

%display summary to console
data
sortScore = sum(data(:,5));
sortPerc = (sum(data(:,5))/210)*100;
disp(strcat('Sort: ',int2str(sortScore),'/210 ---- ',int2str(sortPerc),'%'))
recScore = sum(data(:,6));
recPerc = (sum(data(:,6))/210)*100;
disp(strcat('Recognition: ',int2str(recScore),'/210 ---- ',int2str(recPerc),'%'))

%output all to text file - space delimited
dataFile = strcat('tsm',int2str(sub_num),'.txt');
dlmwrite(dataFile, data, ' ');