MATLAB Programming/Psychtoolbox/Example Code
Back to MATLAB Programming/Psychtoolbox
Examples
edit- Imageviewer - show an image on the screen (OSX)
- OpenManyScreens - open a window on all connected displays (OSX)
- xsearch - generic visual search code (OS9/WIN)
- ImgShow - loads a series of images from disk and displays them for the period of time specified, in random order (OS9/WIN)
- eyelink toolbox - use an sr-research eyelink video eyetracker together with the cross-platform opengl psychtoolbox (OSX/win)
Helpful scripts
edit- MATLAB Programming/Psychtoolbox/Version independent scripts - Scripts to allow you to write programs that run on all versions of psychtoolbox.
Code Snippets
edit- Function that takes a list of desired keys and returns the number of the one that was pressed and is nearer to the front of the list that was passed. It ignores other keypresses.
function keyPressed = getKeys(keysWanted) % keysWanted is a matrix list of keys you are % waiting for e.g, [124 125 kbName('space')] FlushEvents('keydown'); success = 0; while success == 0 pressed = 0; while pressed == 0 [pressed, secs, kbData] = KbCheck; end; for i = 1:length(keysWanted) if kbData(keysWanted(i)) == 1 success = 1; keyPressed = keysWanted(i); FlushEvents('keydown'); break; end; end; FlushEvents('keydown'); end;
- Wait for a key press, continue after two seconds
tEnd=GetSecs+2; while ~KbCheck & GetSecs<tEnd % Sleep one millisecond after each check, so we don't % overload the system in Rush or Priority > 0 WaitSecs(0.001); end
- Bring command window to front. Even though it won't come in front of a window opened by Screen, it is useful to include this, as it allows you to type e.g. Screen('Closeall') to close the window of a crashed program
commandwindow;
- Issue: KbCheck doesn't appear to wait for key presses, exits loop earlier then expected, continues to return key presses. Solution: make sure you first wait for any keys to be released. For example:
commandwindow; touch=1; while touch==1 [touch, secs, keycode] = KbCheck; % Sleep one millisecond after each check, so we don't % overload the system in Rush or Priority > 0 WaitSecs(0.001); end touch = 0; i=0; while ~touch % Sleep one millisecond after each check, so we don't % overload the system in Rush or Priority > 0 WaitSecs(0.001); [touch, secs, keycode] = KbCheck; fprintf('.'); if i==50 fprintf('\n'); i=0; end i=i+1; end clc; % clear command window, removing any typed characters fprintf('\ntouch down\n\n');