Difference between revisions of "Matlab"

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


==Framework==
===[[Framework]]===
===Loops===
See the Shape Search example to get a fleshed out version of this code
<source lang="matlab">


numberExperiments = 2;
===[[Display]]===
numberBlocks = 4;
numberTrials = 48;
%content that is shown at the beginning of the session only goes here
experiment = 1;
while (experiment <= numberExperiments)
    %content that gets shown before each experiment goes here
    block = 1;
    while (block <= numberBlocks)
        %content that gets shown before each block goes here
        trial = 1;
        while (trial <= numberTrials)
            %content that gets shown on each trial goes here
            trial = trial + 1;
        end
        block = block + 1;
    end
    experiment = experiment + 1;
end
</source>


===Screens===
===[[Input / Output]]===
<source lang="matlab">
bgColor = [255 255 255];
window = Screen('OpenWindow', 0, bgColor);
    %experiment goes here
Screen(window, 'Close');
</source>


===Show / hide mouse cursor===
===[[Psychology Experiment Specific]]===
<source lang="matlab">
hidecursor
  %experiment goes here
showcursor
</source>


===Example: using loops to control blocks and trials===
===[[Eyelink_1000_Eye-Tracking_System#Matlab_Code|Eyelink]]===
This example code will loop through the number of experiments, blocks and trials that you set, and display which iteration it is currently on
<source lang="matlab">
bgColor = [255 255 255];
window = Screen('OpenWindow', 0, bgColor);
numberExperiments = 2;
numberBlocks = 3;
numberTrials = 5;
Screen('DrawText', window, 'Press any key to start', xCent, yCent);
Screen(window, 'Flip');
kbwait();
experiment = 1;
while (experiment <= numberExperiments)
    %content that gets shown before each experiment goes here
    block = 1;
    while (block <= numberBlocks)
        Screen('DrawText', window, ['block number: ', int2str(block)], 500, 500);
        Screen(window, 'Flip');
        pause(1);
        trial = 1;
        while (trial <= numberTrials)
            %content that gets shown on each trial goes here
            trial = trial + 1;
        end
        block = block + 1;
    end
    experiment = experiment + 1;
end
Screen(window, 'Close');


</source>
===[[Full Examples]]===


==Display==
See our [[Script Repository]] for some MATLAB scripts.
===Positioning===
===Show text onscreen===
===Loading images into MATLAB===
===Making Textures===
===Showing images on screen===
===Screen Flip===
===Example: displaying onscreen images===


==Input / Output==
==Additional Resources==
===Wait for user input===
[[Quick notes]]
===Check input value===
===Saving out data===
===Example: save data to a text file===


==Psychology Experiment Specific==
[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.
===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
<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