<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>ScannerSyncMatlab</title><revhistory><revision><revnumber>9</revnumber><date>2013-03-08 10:28:25</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>8</revnumber><date>2009-08-20 15:00:42</date><authorinitials>AnnikaLinke</authorinitials></revision><revision><revnumber>7</revnumber><date>2009-03-19 12:40:59</date><authorinitials>AnnikaLinke</authorinitials></revision><revision><revnumber>6</revnumber><date>2009-02-28 15:31:31</date><authorinitials>AnnikaLinke</authorinitials></revision><revision><revnumber>5</revnumber><date>2009-02-27 23:43:41</date><authorinitials>AnnikaLinke</authorinitials></revision><revision><revnumber>4</revnumber><date>2009-02-25 10:19:48</date><authorinitials>RhodriCusack</authorinitials></revision><revision><revnumber>3</revnumber><date>2009-02-25 10:18:43</date><authorinitials>RhodriCusack</authorinitials></revision><revision><revnumber>2</revnumber><date>2009-02-25 10:03:44</date><authorinitials>RhodriCusack</authorinitials></revision><revision><revnumber>1</revnumber><date>2009-02-25 10:03:14</date><authorinitials>RhodriCusack</authorinitials></revision></revhistory></articleinfo><para><emphasis><emphasis role="strong"> OTHER PAGES IN THIS SECTION: </emphasis></emphasis> </para><itemizedlist><listitem><para><ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSync#">Introduction</ulink> </para></listitem><listitem><para><ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSyncDocumentation#">Principles</ulink> </para></listitem><listitem><para><ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSyncCommandReference#">ScannerSyncCommandReference</ulink> </para></listitem><listitem><para><ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSyncExamples#">ScannerSyncExamples</ulink> </para></listitem><listitem><para><ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSyncMatlab#">ScannerSyncMatlab</ulink> </para></listitem></itemizedlist><section><title>Using ScannerSync from Matlab</title><para>Make sure you have the <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSync#">ScannerSync</ulink> control installed. </para><para>Somewhere near the top of your code, set up <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSync#">ScannerSync</ulink> &amp; initialise communication with the input-output board (will not work if you're using a machine without the board, e.g., not the mimic or stim delivery machines). </para><screen><![CDATA[%% SCANNERSYNC
TR=1000; % TR in ms
numdummies=8; 
]]><![CDATA[
% Create & initialise scanner sync object
objSS=actxserver('MRISync.ScannerSync');  %Create a scanner object
invoke(objSS,'Initialize','') %Initialise the Keithley board and object
invoke(objSS,'SetMSPerSample',2); %Set StartExperiment routines’ sampling ]]></screen><para>Now, wait for first pulse from scanner. Also, tell <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSync#">ScannerSync</ulink> the approximate TR. </para><screen><![CDATA[% ScannerSync - waits for the first pulse, resets timer to 0, sets TR
invoke(objSS,'StartExperiment', double(TR));]]></screen><para>Wait for dummies (one less as <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/StartExperiment#">StartExperiment</ulink> command above will already have heard a pulse) </para><screen><![CDATA[for countdown=(numdummies-1):-1:1
    count_text=sprintf('%d', countdown);
  % uncomment the next two if you're using the PsychToolbox
%    DrawFormattedText(window, count_text, 'center', 'center', white);
%    Screen(window, 'Flip');
    invoke(objSS,'SynchroniseExperiment',1,0); %  1= force wait for actual pulse; 0=return this many ms after pulse
end; ]]></screen><para>In your trial loop, you'll need to spend some of the time listening for pulses </para><screen><![CDATA[    invoke(objSS,'CheckPulseSynchronyForTime', double(500)); % spend 500 ms listening for any pulses]]></screen><para>You should dump out the timing of critical events (e.g. starttime of a trial) and the measured TR </para><screen><![CDATA[    starttime=invoke(objSS,'SSGetTimer');
    measuredTR=invoke(objSS,'GetMeasuredTR');
]]><![CDATA[
% ...add your own code to write these to your output file]]></screen><para>Also, for some designs you may want to occasionally synchronise your trials to the scanner </para><screen><![CDATA[    invoke(objSS,'SynchroniseExperiment',1,0); %  1= force wait for actual pulse; 0=return this many ms after pulse]]></screen><section><title>Using pretend mode</title><para>If you'd like to test code on a machine without the card, you may use a feature of <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/ScannerSync#">ScannerSync</ulink> called &quot;pretend mode&quot;. To do this: </para><para>(1) When in pretend mode, don't do &quot;Initialise&quot;, and instead issue a &quot;<ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/SetPretendMode#">SetPretendMode</ulink>&quot; command. So change the first block to something like </para><screen><![CDATA[pretendmode=1;
% Create & initialise scanner sync object
objSS=actxserver('MRISync.ScannerSync');  %Create a scanner object
if (~pretendmode)
  invoke(objSS,'Initialize','') %Initialise the Keithley board and object
else
  invoke(objSS,'SetPretendMode',1) 
end;
invoke(objSS,'SetMSPerSample',2); %Set StartExperiment routines’ sampling ]]></screen><para>(2) You'll then see a warning message when you get to the &quot;<ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/StartExperiment#">StartExperiment</ulink>&quot; command, to which you should click &quot;OK&quot;. If you're using the <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/PsychToolbox#">PsychToolbox</ulink> and have already set up the screen, make sure that you only use the <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/HideCursor#">HideCursor</ulink> command after the <ulink url="https://lsr-wiki-01.mrc-cbu.cam.ac.uk/methods/ScannerSyncMatlab/methods/StartExperiment#">StartExperiment</ulink> command, otherwise you won't be able to click OK. </para><para>If you would like to use the button box in the practice room of the Scanner building, you will need this code instead to make the button box work:  </para><screen><![CDATA[pretendmode=1;
% Create & initialise scanner sync object
objSS=actxserver('MRISync.ScannerSync');  %Create a scanner object
if (~pretendmode)
  invoke(objSS,'Initialize','') %Initialise the Keithley board and object
else
  invoke(objSS,'SetPretendModeExtended',1,0) 
  invoke(objSS,'Initialize','') %Initialise the Keithley board and object 
end;
invoke(objSS,'SetMSPerSample',2); %Set StartExperiment routines’ sampling ]]></screen></section><section><title>Collecting Responses</title><para>To collect responses in matlab using the button box add a code similar to this: </para><screen><![CDATA[ tic
 resp=0;
 gotresp=false;
   while toc<rt_window
      if (~gotresp)
         resp=bitand(30,invoke(objSS,'GetResponse'));
         if (resp==samekey || resp==diffkey)
            gotresp=true;
         end;
       end;
    end;]]></screen><para>This will collect the responses made by the subject during a pre-defined time interval (rt_window). You will also need to define which buttons the subject should press (in this example they are called samekey and diffkey).  </para></section></section></article>