Using ScannerSync from Matlab - NEW METHOD

Open Matlab and check that the ScannerSynchClass is on the path, e.g. using:

which ScannerSynchClass

This should return the path to the ScannerSynchClass.m file. On the stim machines, this should be C:\Program Files\MATLAB\R2014a\toolbox\CBSU.

If you get an error message like "ScannerSynchClass not found", try:

addpath('C:\Program Files\MATLAB\R2014a\toolbox\CBSU')

Once the ScannerSynchClass is available, create an instance of the class:

SSO=ScannerSynchClass

Once the instance has been created, you can use the class properties and methods to collect pulses and button box responses:

== Properties (internal variables) ==
IsValid         = device set and operational
TR              = set a non-zero value (in seconds) for emulation mode (will not detect "real"          pulses)
Keys    = set a cell of key names for emulation mode. For key names look for KbName.m.
        N.B.: Requires PTB.
        N.B.: Suppress passing keypresses to MATLAB during the whole experiment

Clock   = interal clock (seconds past since the first scanner pulse or clock reset)
Synch   = current state of the scanner synch pulse
TimeOfLastPulse         = time (according to the internal clock) of the last pulse 
MeasuredTR      = estimated TR
SynchCount      = number of scanner synch pulses
MissedSynch     = number of missed scanner synch pulses

EmulSynch       = is scanner synch pulse emulated 
EmulButtons     = is button box emulated
%
Buttons                 = current state of the any button
LastButtonPress         = index/indices of the last button(s) pressed
TimeOfLastButtonPress   = time (according to the internal clock) of the last button press (any) 
BBoxTimeout     = set a non-Inf value (in seconds) to wait for button press only                               = set a negative value (in seconds) to wait even in case of response

== Methods (internal functions) ==
ScannerSynchClass       = constructor
delete  = destructor
ResetClock      = reset internal clock

ResetSynchCount         = reset scanner synch pulse counter
SetSynchReadoutTime(t)  = blocks scanner synch pulse readout after a pulse for 't' seconds
WaitForSynch    = wait until a scanner synch pulse arrives
CheckSynch(t)   = wait for a scanner synch pulse for 't' seconds or unitl a scanner synch pulse arrives (whichever first) and returns whether a scanner synch pulse was detected

        SetButtonReadoutTime(t)         = blocks individual button readout after a button press for 't' seconds (detection of other buttons is still possible) 
SetButtonBoxReadoutTime(t)      = blocks the whole button box readout after a button press for 't' seconds (detection of other buttons is also not possible) 
WaitForButtonPress                      = wait until a button is pressed
WaitForButtonRelease            = wait until a button is released



%
% USAGE
%
% Initialise:
%       SSO = ScannerSynchClass;
%       % SSO = ScannerSynchClass(1);   % emulate scanner synch pulse
%       % SSO = ScannerSynchClass(0,1); % emulate button box
%       % SSO = ScannerSynchClass(1,1); % emulate scanner synch pulse and button box
%
% Close:
%       SSO.delete;
%
% Example for scanner synch pulse #1: - Simple case
%       SSO.SetSynchReadoutTime(0.5);
%       SSO.TR = 2;                % allows detecting missing pulses
%       while SSO.SynchCount < 10  % polls 10 pulses
%       SSO.WaitForSynch;
%       fprintf('Pulse %d: %2.3f. Measured TR = %2.3fs\n',...
%           SSO.SynchCount,...
%           SSO.TimeOfLastPulse,...
%           SSO.MeasuredTR);
%       end
%
% Example for scanner synch pulse #2 - Chance for missing pulse
%       SSO.SetSynchReadoutTime(0.5);
%       SSO.TR = 2;                    % allows detecting missing pulses
%       while SSO.SynchCount < 10      % until 10 pulses
%       WaitSecs(Randi(100)/1000); % in every 0-100 ms ...
%       if SSO.CheckSynch(0.01)    % ... waits for 10 ms for a pulse
%               fprintf('Pulse %d: %2.3f. Measured TR = %2.3fs. %d synch pulses has/have been missed\n',...
%               SSO.SynchCount,...
%               SSO.TimeOfLastPulse,...
%               SSO.MeasuredTR,...
%               SSO.MissedSynch);
%       end
%       end
%
% Example for buttons:
%       SSO.SetButtonReadoutTime(0.5);      % block individual buttons
%       % SSO.SetButtonBoxReadoutTime(0.5); % block the whole buttonbox
%   % SSO.Keys = {'f1','f2','f3','f4'}; % emulation Buttons #1-#4 with F1-F4
%       n = 0;
%   % SSO.BBoxTimeout = 1.5;            % Wait for button press for 1.5s
%   % SSO.BBoxTimeout = -1.5;           % Wait for button press for 1.5s even in case of response
%       SSO.ResetClock;
%       while n ~= 10                       % polls 10 button presses
%       SSO.WaitForButtonPress;         % Wait for any button to be pressed
%       SSO.WaitForButtonRelease;       % Wait for any button to be     released
%       % SSO.WaitForButtonPress([],2); % Wait for Button #2
%       % SSO.WaitForButtonPress(2);    % Wait for any button for 2s (overrides SSO.BBoxTimeout only for this event)
%       % SSO.WaitForButtonPress(-2);   % Wait for any button for 2s even in case of response (overrides SSO.BBoxTimeout only for this event)
%       % SSO.WaitForButtonPress(2,2);  % Wait for Button #2 for 2s (overrides SSO.BBoxTimeout only for this event)
%       % SSO.WaitForButtonPress(-2,2); % Wait for Button #2 for 2s even in case of response (overrides SSO.BBoxTimeout only for this event)
%       n = n + 1;
%       fprintf('At %2.3fs, ',SSO.Clock);
%       fprintf('Button %d ',SSO.LastButtonPress);
%       fprintf('pressed: %2.3fs\n',SSO.TimeOfLastButtonPress);
%       end