Add persistent user-defined paths to Matlab and Octave

Matlab R2016a added the .mltbx toolbox packaged toolbox format that is proprietary to Matlab. Oftentimes we desire to distribute a Matlab package as a set of .m source files instead. To add a “toolbox” or “package” to Matlab and use functions from that toolbox requires using “addpath” or “import” Matlab syntax. This example makes those paths persistent in Matlab and Octave, using example toolbox directories ~/mypkg1 and ~/mypkg2.

Normally use addpath() instead of cd(). Do not put brackets or braces around the multiple paths.

Prepend your path(s) by adding to file ~/Documents/MATLAB/startup.m (this also works with GNU Octave).

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

Restart Matlab/Octave 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 by adding to ~/.bashrc

export MATLAB_USE_USERWORK=1