[Recipes] Registering fMRI volume triggers with PTB3
There are several ways one can go about reading triggers from a MRI scanner, with the PsychToolbox, (see also Peter Scarfe’s tutorials), and they can be slightly confusing. Below is a cookbook of a few ways that have worked for me.
Timing with MEX files
The first time you access any MEX function or M file, Matlab takes several hundred milliseconds to load it from disk. Allocating a variable takes time too. Usually you’ll want to omit those delays from your timing measurements by making sure all the functions you use are loaded and that all the variables you use are allocated, before you start timing. MEX files stay loaded until you flush the MEX files (e.g. by changing directory or calling CLEAR MEX). M files and variables stay in memory until you clear them.
This basically means that any function call will require matlab to load a series of files, and this operation takes time, before you’ll even start using the function. This may or may not be a problem. If you require high precision timing, you will need to assert timing for the important portions of your code (e.g. by using the GetSecs function).
Situations that may require this extra step may be when you are interested in the time delta between triggers, want to compute some average over time (think EEG/ERP), etc. If you need to use a PTB function during a time-critical period, then you’ll have to preload the associated MEX file, by simply calling the function once, before the portion of code that is time-critical.