A copy is also included in PsychBench in <PsychBench folder>/demos, so you can run it from there.
<cdcm>% SCANNER, SYNC IN EACH BLOCK DEMO<cdcm>
<cdcm>% Coding method<cdcm>
<cdcm>% --------------------------------------------------<cdcm>
<cdcm>% Each trial shows one of three pictures and the subject responds with any key<cdcm>
<cdcm>% press. Trials start at fixed times from sync trigger from a scanner at the<cdcm>
<cdcm>% start of each block: 0.75 sec for the first trial after sync, and 5 sec<cdcm>
<cdcm>% increments for subsequent trials. In each trial the picture shows until the<cdcm>
<cdcm>% subject responds or until the pre-trial interval for the next trial needs to<cdcm>
<cdcm>% start, whichever occurs first. In each block we run 4 repetitions of each of<cdcm>
<cdcm>% the 3 picture trials for a total of 12 trials, all in random order. We run 2<cdcm>
<cdcm>% blocks and sync at the start of each one.<cdcm>
<cdcm>%<cdcm>
<cdcm>% This scanner demo syncs in each block of trials, trial timing is relative to<cdcm>
<cdcm>% sync, and element timing is relative to trial. See also scannerSyncInTrialsDemo.m.<cdcm>
<cdcm>% Mark start of experiment script<cdcm>
newExperiment
<cdcm>% Define trial containing keyPress object to register trigger and sync experiment<cdcm>
<cdcm>% -<cdcm>
<cdcm>% Simulate trigger by key press for this tutorial.<cdcm>
<cdcm>% Many scanners send triggers as key presses, in which case would use a keyPress object in a real experiment too.<cdcm>
<cdcm>% By default listens for any key (could be changed in property .listenKeyNames).<cdcm>
syncer = keyPressObject;
<cdcm>% Listen for key press from default keyboard just for this demo.<cdcm>
<cdcm>% In a real experiment would need to set this to point to whatever device the trigger is coming from.<cdcm>
syncer.n_device = [];
<cdcm>% Register input as a trigger instead of response from subject<cdcm>
syncer.registerTrigger = true;
<cdcm>% Sync experiment at trigger so can set subsequent trials to start at times from sync<cdcm>
syncer.syncExperiment = true;
<cdcm>% Default start at trial start.<cdcm>
<cdcm>% By default keyPress elements end when they record a trigger/response -> don't need to set .end.<cdcm>
<cdcm>% Report sync time ( = trigger time)<cdcm>
syncer.report = <cdsm>"syncTime"<cdsm>;
<cdcm>% Define trial with custom label "sync"<cdcm>
addTrial(syncer, <cdsm>"sync"<cdsm>);
<cdcm>% -<cdcm>
<cdcm>% 3 task trial definitions: 3 pictures<cdcm>
fileNames = [<cdsm>"sphere.png"<cdsm> <cdsm>"cube.png"<cdsm> <cdsm>"hourglass.png"<cdsm>];
<cdkm>for<cdkm> f = 1:3
<cdcm>% Picture stimulus.<cdcm>
<cdcm>% Start at trial start.<cdcm>
<cdcm>% End at response.<cdcm>
pic = pictureObject;
pic.fileName = fileNames(f);
pic.height = 8;
pic.end.response = true;
pic.report = [<cdsm>"fileName"<cdsm> <cdsm>"startTime"<cdsm>];
<cdcm>% Response handler - any key press, this time for response from subject.<cdcm>
<cdcm>% Start at trial start.<cdcm>
<cdcm>% By default ends on its own at response.<cdcm>
recorder = keyPressObject;
recorder.report = <cdsm>"responseTime"<cdsm>;
<cdcm>% Set trial to start at fixed time relative to sync in a past trial using trial object property .start field .t_sync.<cdcm>
<cdcm>% 0.75 sec from sync if this is the first trial after it, 5 sec increments for subsequent trials.<cdcm>
<cdcm>% Refers to the order trials run in set in trial list below.<cdcm>
<cdcm>% - Prevents timing drift which could occur with default flexible trial timing<cdcm>
<cdcm>% - Will wait to start if previous trial ends early<cdcm>
<cdcm>% - Will end previous trial if it would run late<cdcm>
<cdcm>% - Will also end this trial similarly if it's the last one in a sequence with .start.t_sync set<cdcm>
trial = trialObject;
trial.start.t_sync = [0.75 5];
<cdcm>% Note pre-trial interval still needs to run, so check not set so long that previous trial gets cut off early.<cdcm>
<cdcm>% e.g. 0.75 sec maximum (first trial after sync), or 5 - 2 = 3 sec maximum (subsequent trials).<cdcm>
<cdcm>% Default pre-trial interval = 0.75 sec, or can change in trial object property .preTrialInterval.<cdcm>
<cdcm>% Add trial definition with default numbering (1, 2, 3, ...)<cdcm>
addTrial(pic, recorder, trial);
<cdkm>end<cdkm>
<cdcm>% Set trial list:<cdcm>
<cdcm>%<cdcm>
<cdcm>% 1. sync with scanner<cdcm>
<cdcm>% 2. block of 4 x task trial definitions in random order<cdcm>
<cdcm>% 3. re-sync with scanner<cdcm>
<cdcm>% 4. block of 4 x task trial definitions in random order<cdcm>
nn = {<cdsm>"sync"<cdsm> randomOrder(rep(1:3, 4)) <cdsm>"sync"<cdsm> randomOrder(rep(1:3, 4))};
setTrialList(nn)
<cdcm>% Run<cdcm>
[results, resultsMatrix] = runExperiment;
<cdcm>% See also:<cdcm>
<cdcm>% - element types cedrusPress, responsepixxPress, etc. for button boxes<cdcm>
<cdcm>% - pb_prefs() -> screen tab -> flip horizontal/vertical (screen object<cdcm>
<cdcm>% properties .flipHorz/flipVert) for display through a mirror<cdcm>