Difference between revisions of "Matlab"

From REALab Wiki
Jump to navigation Jump to search
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
Here is the new matlab script page!


==Framework==
===[[Framework]]===
===Loops===
<source lang="matlab">


%main loop
===[[Display]]===
experiment = 1;
while (experiment <= numberExperiments)
    if experiment ~= 1
        if set == 1
            set = 2;
        elseif set == 2
            set = 1;
        end
        if choice == 1
            choice = 0;
        elseif choice == 0
            choice = 1;
        end
    end
    block = 1;
    clear shapeset shape1 shape2 shape3 shape4
    if set == 1
        Img_set1=imread('images/set1.png'); %load set1 choice screen
        shapeset = Screen('MakeTexture', window, Img_set1);
        Img_plusBlack=imread('images/Plus.png'); %load black plus
        shape1 = Screen('MakeTexture', window, Img_plusBlack);
        Img_squareBlack=imread('images/Square.png'); %load black square
        shape2 = Screen('MakeTexture', window, Img_squareBlack);
        Img_starBlack=imread('images/Star.png'); %load black star
        shape3 = Screen('MakeTexture', window, Img_starBlack);
        Img_triangleBlack=imread('images/Triangle.png'); %load black triangle
        shape4 = Screen('MakeTexture', window, Img_triangleBlack);
    elseif set == 2
        Img_set2=imread('images/set2.png'); %load set2 choice screen
        shapeset = Screen('MakeTexture', window, Img_set2);
        Img_circleBlack=imread('images/Circle.png'); %load black circle
        shape1 = Screen('MakeTexture', window, Img_circleBlack);
        Img_diamondBlack=imread('images/Diamond.png'); %load black diamond
        shape2 = Screen('MakeTexture', window, Img_diamondBlack);
        Img_exBlack=imread('images/Ex.png'); %load black ex
        shape3 = Screen('MakeTexture', window, Img_exBlack);
        Img_hexagonBlack=imread('images/Hexagon.png'); %load black hexagon
        shape4 = Screen('MakeTexture', window, Img_hexagonBlack);
    end
    %define the target for no choice condition
    if choice == 0
        targetChoice = randi(1,4);
        targetShape = strcat('shape',int2str(targetChoice(1)));
        Screen('DrawText', window, 'Your target is now...', xCent, yCent-100);
        Screen('DrawTexture', window, eval(targetShape), [], positionC);
        Screen(window, 'Flip');
        pause(3);
    elseif choice == 1 %choose target for choice condition
        Screen('DrawTexture', window, shapeset, [], [0 0 screen_size(3) screen_size(4)]);
        Screen(window, 'Flip');
        targetChoice = str2num(getchar);
        targetShape = strcat('shape',int2str(targetChoice));
    end
   
    while (block <= numberBlocks)
        trial = 1;
        Screen('DrawText', window, ['block number: ', int2str(block)], xCent, yCent);
        Screen(window, 'Flip');
        score = 0;
        pause(1);
        while (trial <= numberTrials)
            %show fixation
            Screen('DrawText', window, '+', xCent, yCent);
            Screen(window, 'Flip');
            pause(fixationTime);
            %draw target shape in random position
            shapeOrder = randperm(4);
            Screen('DrawTexture', window, eval(targetShape), [], eval(strcat('position',int2str(shapeOrder(1)))));
            %draw other 3 shapes in random order depending on what target
            %shape is
            if targetChoice == 1
                Screen('DrawTexture', window, shape2, [], eval(strcat('position',int2str(shapeOrder(2)))));
                Screen('DrawTexture', window, shape3, [], eval(strcat('position',int2str(shapeOrder(3)))));
                Screen('DrawTexture', window, shape4, [], eval(strcat('position',int2str(shapeOrder(4)))));
            elseif targetChoice == 2
                Screen('DrawTexture', window, shape1, [], eval(strcat('position',int2str(shapeOrder(2)))));
                Screen('DrawTexture', window, shape3, [], eval(strcat('position',int2str(shapeOrder(3)))));
                Screen('DrawTexture', window, shape4, [], eval(strcat('position',int2str(shapeOrder(4)))));
            elseif targetChoice == 3
                Screen('DrawTexture', window, shape1, [], eval(strcat('position',int2str(shapeOrder(2)))));
                Screen('DrawTexture', window, shape2, [], eval(strcat('position',int2str(shapeOrder(3)))));
                Screen('DrawTexture', window, shape4, [], eval(strcat('position',int2str(shapeOrder(4)))));
            elseif targetChoice == 4
                Screen('DrawTexture', window, shape1, [], eval(strcat('position',int2str(shapeOrder(2)))));
                Screen('DrawTexture', window, shape2, [], eval(strcat('position',int2str(shapeOrder(3)))));
                Screen('DrawTexture', window, shape3, [], eval(strcat('position',int2str(shapeOrder(4)))));
            end
            Screen(window, 'Flip');
            startTime = GetSecs;
            %determine the correct input response based on target position
            switch shapeOrder(1)
                case 1
                    correctResponse = 'w'; %???
                case 2
                    correctResponse = 'd';
                case 3
                    correctResponse = 's';
                case 4
                    correctResponse = 'a';
            end
            %get response and record RT
            response = getchar;
            reactionTime(trial) = GetSecs - startTime;
            if response == correctResponse
                correct = 1;
                Screen('DrawText', window, 'correct', xCent, yCent);
                Screen('DrawText', window, strcat('RT = ',num2str(reactionTime(trial),3),'s'), xCent, yCent+20);
                Screen(window, 'Flip');
                pause(1)
            else
                correct = 0;
                Screen('DrawText', window, 'wrong', xCent, yCent);
                Screen('DrawText', window, strcat('RT = ',num2str(reactionTime(trial),3),'s'), xCent, yCent+20);
                Screen(window, 'Flip');
                pause(1)
            end
            %save data
            data(trial,1) = sub_num; %???
            data(trial,2) = experiment;
            data(trial,3) = trial;
            data(trial,4) = correct;
            data(trial,5) = reactionTime(trial);
           
            fprintf(dataFile, 'data(trial,1:)');
           
            score = score + correct;
            trial = trial + 1;
        end
        averageRT = mean(reactionTime);
        accuracyScore = ((score/numberTrials)*100);
        Screen('DrawText', window, 'End of Block', xCent-20, yCent-20);
        Screen('DrawText', window, strcat('Average reaction time = ',num2str(averageRT,3),'s'), xCent-20, yCent);
        Screen('DrawText', window, strcat('Accuracy = ',int2str(accuracyScore),'%'), xCent-20, yCent+20);
        Screen('DrawText', window, 'Press spacebar to continue' , xCent-20, yCent+100);
        Screen(window, 'Flip');
        input = getKeys([KbName('space')]);
       
        %wait for keypress before continuing
        while 1
            if input == KbName('space')
                break
            end
        end
       
        block = block + 1;
    end
    if experiment ~= numberExperiments
        Screen('DrawText', window, 'Starting next experiment', xCent, yCent);
        Screen(window, 'Flip');
        pause(2)
    end
    experiment = experiment + 1;
end
</source>


===Screens===
===[[Input / Output]]===
===Show / hide mouse cursor===
===Example: using loops to control blocks and trials===


==Display==
===[[Psychology Experiment Specific]]===
===Positioning===
===Show text onscreen===
===Loading images into MATLAB===
===Making Textures===
===Showing images on screen===
===Screen Flip===
===Example: displaying onscreen images===


==Input / Output==
===[[Eyelink_1000_Eye-Tracking_System#Matlab_Code|Eyelink]]===
===Wait for user input===
===Check input value===
===Saving out data===
===Example: save data to a text file===


==Psychology Experiment Specific==
===[[Full Examples]]===
===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==
See our [[Script Repository]] for some MATLAB scripts.
under construction...


==Full Examples==
==Additional Resources==
===Shape Search===
[[Quick notes]]
===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
[http://books.google.ca/books?id=FZZ76H5pCdAC&pg=PA1&lpg=PA1&dq=matlab+for+behavioral+scientists&source=bl&ots=_lkZSVjLsa&sig=B3ZAK4Gt3mCz80AA4zI1ApaIfi8&hl=en&sa=X&ei=XCiIUdD8GK7ligLOl4GICw&ved=0CD0Q6AEwAg Google Books' full text copy of David A. Rosenbaum's "MatLab for Behavioral Scientists"] A detailed and very helpful account of MatLab's uses specifically in psychology.
<source lang="matlab">
%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, ' ');
</source>

Latest revision as of 22:52, 30 March 2015