Gardner Lab

Psychology Department

Neurosciences Institute

Stanford University


Initial setup

  1. Add the mgl directory to your Matlab path. In Matlab:
    >> addpath(genpath('MYPATH/mgl'));

    where MYPATH should be replaced by the path to your version of mgl.

  2. Make sure to enable access for assistive devices so that you can use the function mglGetKeyEvent, mglGetMouseEvent, mglPostEvent, mglSimulateRun and mglEatKeys.
  3. There are a few functions (mglEditScreenParams and mglDoRetinotopy) that require the mrTools GUI functions to be installed. If you are not already using mrTools, you can download the following:
      svn checkout mrToolsUtilities

    and add that to your MATLAB path (with MYPATH replaced with the path to your version of the mrToolsUtilities):

    >> addpath(genpath('MYPATH/mrToolsUtilities'));

Test your setup

You can see what functions are available by doing (in matlab):

>> help mgl

After downloading, you may wish to try the mglTest programs (e.g. mglTestDots, mglTestTex, etc….).

If these functions don't work or you are running on Linux (version 1.5 only), then you may need to recompile. This may especially be necessary if you are running an older version of matlab (we run Matlab version >= 7.3 on Mac OS >= 10.4.8). We have found that mex files created on Matlab 7.3 do not run on matlab 14.1 for instance (if you run -nojvm you will see that it complains that it cannot find a dynamic link library for the mex functions – if you run with the matlab desktop it will just crash the system). If this happens to you simply recompile and you should be good to go.

What is in the mgl distribution

  • mgl/mgllib: The main distribution that has all functions for displaying to the screen.
  • mgl/task: A set of higher level routines that set up a structure for running tasks and trials. Relies on functions in mgl/mgllib. You do no need to use any of these functions if you just want to use this library for drawing to the screen.
  • mgl/utils: Various utility functions.

GNU General Public License

These programs are free to distribute under the GNU General Public License. See the file mgl/COPYING for details.

Recompiling mgl

If you are running into an obvious error like a segmentation fault, then you may want to recompile. The command to recompile is:

» mglMake(1);

If you run into any problems with mglMake, you may want to restart matlab and try again.

The mglMake command simply runs mex on all the .c files in the mgl/mgllib directory – you can do this by hand (e.g. mex mglPrivateOpen.c), if you prefer.

Note that this requires mex to be setup properly on your machine. On a Mac OS X machine, at a minimum you will need to have the apple developer tools installed (XCode) On linux, you will need a compatible version of gcc (older versions of gcc can be found here).

See here for more help on compiling.

If all else fails, how to get back control over the display?

If you can't do mglClose, you can always press:


this will quit your matlab session as well.

Also, some of our test programs will run until you hit the <ESC> key (e.g. testExperiment), so it is worth trying that as well.

Can I get access to all OpenGL functions?

We have only exposed parts of the OpenGL functionality. If you need to dig deeper to code your stimulus, consider writing your own mex file. This will allow you to use the full functionality of the OpenGL library. To do this, you could start by modifying one of our mex functions (e.g. mglClearScreen.c) and add your own GL code to do what you want and compile.

Printing the wiki help pages

You can print out all the wiki help pages at once, by using this link.

Compatibility with latest MAC OS and Matlab versions

We stay reasonably up-to-date with OS and Matlab versions. As of this writing (12/08/2014) we are using MGL on Mac OS 10.9.5 and Matlab 8.2 on 64 bit. If you are using a newer or very much older version of MGL, you may need to recompile. Note that the Eyelink and NI Digital I/O code may particularly have compatibility issues as they rely on libraries made by their manufacturers. As of this writing Eyelink has a 64 bit library which we run that works. NI Digital I/O is still basically 32 bit.

Note, at this time (12/8/2014) Yosemite and Matlab (particularly older versions) do not play well together. There are some patches, but we are staying away until Mathworks cleans things up.