I have not used PsychToolbox for 10 years so I don't know its current capabilities for peripheral digital I/O. However, if you are running MATLAB on WIndows you could use some utilities that I have developed to support digital I/O using hardware like an LPT (printer) port card. This utility lets you read or write to external digital lines with latencies of 100 microsec or so. Documentation for using this MATLAB mex plugin can be found at http://usd-apps.usd.edu/coglab/psyc770/IO64.html
Sorry, but if you are running MATLAB on Linux or MacOS this utility will not be of any help to you.
We have usually just synchronized the onset of our Matlab script with the scanner so that they run in parallel. For this, you can use the line below, with 't' being the trigger signal (you might need to change this depending on your setting):