Adding persistent user-defined paths to Matlab and Octave

Oftentimes we add a “toolbox” to Matlab that someone has created and we want to use functions from that toolbox without adding the path over and over in functions. Note: It’s generally better to use addpath() instead of cd(). Here’s how to make those paths persistent in Matlab and Octave.

Here we use the example toolbox directories ~/mypkg1 and ~/mypkg2.

  1. (Octave only): configure ~/.octaverc to share startup.m with Matlab.
  2. Prepend your path(s) by adding to file ~/Documents/MATLAB/startup.m

     addpath('~/mypkg1','~/mypkg2')
    

    Note that you do not put brackets or braces around the multiple paths. 

  3. restart Matlab and type

     path
    

    and you should see your new toolbox directories at the top.

Notes

Matlab doesn’t autoload startup.m

You may have an older version of Matlab that doesn’t automatically load from ~/Documents/MATLAB/startup.m. Fix this with the one-time Terminal command

echo "export MATLAB_USE_USERWORK=1" >> ~/.bashrc