gonogoDemo.m

A copy is also included in PsychBench in <PsychBench folder>/demos, so you can run it from there.

<cdcm>% GO/NO-GO EXPERIMENT DEMO<cdcm>
<cdcm>% Coding method<cdcm>
<cdcm>% --------------------------------------------------<cdcm>
 
 
<cdcm>% A go/no-go task using colored squares. Each trial shows either a red or blue<cdcm>
<cdcm>% square centered on screen (see property settings below for size). The subject<cdcm>
<cdcm>% responds with any key press if the square is blue, else does not respond if<cdcm>
<cdcm>% red. Time limit for response is 1 sec. The subject receives correct/incorrect<cdcm>
<cdcm>% text feedback for 1 sec. A fixation cross shows in inter-trial intervals. We<cdcm>
<cdcm>% run 10x each color trial = 20 trials, all in random order. The experiment<cdcm>
<cdcm>% starts with instructions and 6 training trials: 3 of each color, all in random<cdcm>
<cdcm>% order.<cdcm>
 
 
 
newExperiment
 
 
 
<cdcm>% TRIAL TEMPLATE<cdcm>
<cdcm>% ==========<cdcm>
    <cdcm>% Make objects and set only properties that will be the same in all trials where we will use the template...<cdcm>
 
 
    <cdcm>% Fixation cross<cdcm>
    <cdcm>% ---<cdcm>
    cross = crossObject;
 
    <cdcm>% Leave properties at default for a small white cross<cdcm>
 
    <cdcm>% Run in pre-trial interval<cdcm>
    cross.start.pretrial = true;
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Square<cdcm>
    <cdcm>% ---<cdcm>
    square = rectangleObject;
 
    <cdcm>% 5 deg square<cdcm>
    square.size = 5;
 
    <cdcm>% Default start at trial start; End at response<cdcm>
    square.end.response = true;
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Key press response handler<cdcm>
    <cdcm>% ---<cdcm>
    anyKey = keyPressObject;
 
    <cdcm>% Leave .listenKeyNames at default = listen for any key<cdcm>
 
    <cdcm>% Record response = NaN if no response before object ends so feedback below will also start at no response<cdcm>
    anyKey.recordDefaultResponse = true;
 
    <cdcm>% Default start at trial start.<cdcm>
    <cdcm>% By default keyPress elements also end when they record one response.<cdcm>
    <cdcm>% Add end at duration = 1 sec -> time limit for response.<cdcm>
    anyKey.end.duration = 1;
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Feedback<cdcm>
    <cdcm>% ---<cdcm>
    <cdcm>% Two text objects<cdcm>
    feedbacks = textObject(2);
 
    <cdcm>% Green "CORRECT" starts at correct response, shows for 1 sec<cdcm>
    feedbacks(1).text = <cdsm>"CORRECT"<cdsm>;
    feedbacks(1).color = [0 1 0];
    feedbacks(1).fontSize = 1;
    feedbacks(1).start.correctResponse = true;
    feedbacks(1).end.duration = 1;
 
    <cdcm>% Red "INCORRECT" starts at incorrect response, shows for 1 sec<cdcm>
    feedbacks(2).text = <cdsm>"INCORRECT"<cdsm>;
    feedbacks(2).color = [1 0 0];
    feedbacks(2).fontSize = 1;
    feedbacks(2).start.incorrectResponse = true;
    feedbacks(2).end.duration = 1;
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Add template with name "trial"<cdcm>
    addTemplate(cross, square, anyKey, feedbacks, <cdsm>"trial"<cdsm>);
<cdcm>% ==========<cdcm>
 
 
 
<cdcm>% TASK TRIALS<cdcm>
<cdcm>% ==========<cdcm>
<cdcm>% 2 trial definitions numbered 1-2: one for each color<cdcm>
<cdkm>for<cdkm> n_color = 1:2
    <cdcm>% Start with objects as set in template "trial" above<cdcm>
    [cross, square, anyKey, feedbacks] = getTemplate(<cdsm>"trial"<cdsm>);
 
 
    <cdcm>% Fixation cross<cdcm>
    <cdcm>% ---<cdcm>
    <cdcm>% (Nothing to add)<cdcm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Square<cdcm>
    <cdcm>% ---<cdcm>
        <cdcm>% Set some information to see in experiment results output.<cdcm>
        <cdcm>% Also set RGB color.<cdcm>
 
        square.info.n_color = n_color;
    <cdkm>if<cdkm> n_color == 1
        square.info.colorName = <cdsm>"red"<cdsm>;
        square.color = [1 0 0];
    <cdkm>else<cdkm>
        square.info.colorName = <cdsm>"blue"<cdsm>;
        square.color = [0 0 1];
    <cdkm>end<cdkm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Key press response handler<cdcm>
    <cdcm>% ---<cdcm>
    <cdcm>% Score correct/incorrect using custom scoring equation<cdcm>
    <cdkm>if<cdkm> n_color == 1
        <cdcm>% Red -> correct = no key press (response = NaN)<cdcm>
        anyKey.scoreResponse = <cdsm>"isnan(response)"<cdsm>;
    <cdkm>else<cdkm>
        <cdcm>% Blue -> correct = any key press (response ~= NaN)<cdcm>
        anyKey.scoreResponse = <cdsm>"~isnan(response)"<cdsm>;
    <cdkm>end<cdkm>
 
        <cdcm>% See response, score, latency in results<cdcm>
        anyKey.report = [<cdsm>"response"<cdsm> <cdsm>"responseScore"<cdsm> <cdsm>"responseLatency"<cdsm>];
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Feedback<cdcm>
    <cdcm>% ---<cdcm>
    <cdcm>% (Nothing to add)<cdcm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Add trial definition with default numbering (1, 2, ...)<cdcm>
    addTrial(cross, square, anyKey, feedbacks);
<cdkm>end<cdkm>
<cdcm>% ==========<cdcm>
 
 
 
<cdcm>% TRAINING TRIALS<cdcm>
<cdcm>% ==========<cdcm>
<cdcm>% Same as task trial definitions except no experiment results output and different feedback<cdcm>
<cdkm>for<cdkm> n_color = 1:2
    [cross, square, anyKey, feedbacks] = getTemplate(<cdsm>"trial"<cdsm>);
 
 
    <cdcm>% Square<cdcm>
    <cdcm>% ---<cdcm>
    <cdkm>if<cdkm> n_color == 1
        square.color = [1 0 0];
    <cdkm>else<cdkm>
        square.color = [0 0 1];
    <cdkm>end<cdkm>
 
    <cdcm>% Don't set .info/report -> nothing in results for this object<cdcm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Mouse click response<cdcm>
    <cdcm>% ---<cdcm>
    <cdkm>if<cdkm> n_color == 1
        anyKey.scoreResponse = <cdsm>"isnan(response)"<cdsm>;
    <cdkm>else<cdkm>
        anyKey.scoreResponse = <cdsm>"~isnan(response)"<cdsm>;
    <cdkm>end<cdkm>
 
    <cdcm>% Don't set .info/report -> nothing in results for this object<cdcm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Feedback<cdcm>
    <cdcm>% ---<cdcm>
    <cdcm>% Increase feedback durations for longer text<cdcm>
    feedbacks(1).end.duration = 1.5;
    feedbacks(2).end.duration = 3;
 
    <cdcm>% Change text for incorrect feedback.<cdcm>
    <cdcm>% Strings in an array are separate lines.<cdcm>
    <cdcm>% We use an in-line formatting command to change size for the second line (see text type documentation).<cdcm>
    <cdkm>if<cdkm> n_color == 1
        feedbacks(2).text = [
            <cdsm>"INCORRECT"<cdsm>
            <cdsm>"<size = 0.7>Remember only press a key when you see the blue square..."<cdsm>
            ];
    <cdkm>else<cdkm>
        feedbacks(2).text = [
            <cdsm>"INCORRECT"<cdsm>
            <cdsm>"<size = 0.7>Remember press a key when you see the blue square..."<cdsm>
            ];
    <cdkm>end<cdkm>
    <cdcm>% ---<cdcm>
 
 
    <cdcm>% Add trial definition to group 100 (101, 102, ...)<cdcm>
    addTrial(cross, square, anyKey, feedbacks, 100);
<cdkm>end<cdkm>
<cdcm>% ==========<cdcm>
 
 
 
<cdcm>% INTRO, PAUSE TRIALS<cdcm>
<cdcm>% ==========<cdcm>
<cdcm>% Intro.<cdcm>
<cdcm>% dialogue object to show text until subject presses any key.<cdcm>
<cdcm>% Can just set text and leave all properties at default.<cdcm>
text = dialogueObject;
<cdcm>% Each string in the array is a new line<cdcm>
text.text = [
    <cdsm>"Press a key as fast as you can when you see the <color = [0 0 1]>BLUE<color = [1 1 1]> square."<cdsm>
    <cdsm>"Let's start with a few tries to get the hang of it."<cdsm>
    <cdsm>"Press any key to begin..."<cdsm>
    ];
 
<cdcm>% Add trial definition with name "intro"<cdcm>
addTrial(text, <cdsm>"intro"<cdsm>);
 
 
<cdcm>% Pause.<cdcm>
<cdcm>% Use same object from above, just change the text.<cdcm>
text.text = <cdsm>"Okay! Now let's try it for real..."<cdsm>;
 
<cdcm>% Add trial definition with name "pause"<cdcm>
addTrial(text, <cdsm>"pause"<cdsm>);
<cdcm>% ==========<cdcm>
 
 
 
<cdcm>% Set trial list:<cdcm>
<cdcm>% - intro<cdcm>
<cdcm>% - 3 x 2 training trial definitions in random order<cdcm>
<cdcm>% - pause<cdcm>
<cdcm>% - 10 x 2 task trial definitions in random order<cdcm>
nn = {<cdsm>"intro"<cdsm> randomOrder(rep(101:102, 3)) <cdsm>"pause"<cdsm> randomOrder(rep(1:2, 10))};
setTrialList(nn)
 
 
 
<cdcm>% You can call "viewExperiment" and "viewExperiment -d" to visualize trials<cdcm>
 
 
 
[results, resultsMatrix] = runExperiment;