4

I have downloaded a toolbox with many files in many subfolders (spatial-econometrics toolbox) for use on one particular project and I don't want to add it to the path because I don't think I'm going to make a habit of using it and I don't know if it's going to hide functions say in the stats toolbox.

How do I access the functions within this toolbox? Is there a way to maybe programatically add it to the path just for the particular session of Matlab that the script gets called in? What is the correct way to deal with this?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    Is using a "private" folder as described here a good way? http://stackoverflow.com/questions/1277613/how-do-i-emulate-include-behaviour-in-matlab – Dan Feb 25 '14 at 12:15
  • 3
    addpath is the command for current session usage only, more info - http://www.mathworks.in/help/matlab/ref/addpath.html. savepath is the command for permanent change to path, more info - http://www.mathworks.in/help/matlab/ref/savepath.html – Divakar Feb 25 '14 at 12:16
  • @Divakar but that will permanently add it to the path won't it? Which is not what I want. – Dan Feb 25 '14 at 12:16
  • 1
    also you can use the command - GENPATH, if you need to recursively add the sub-directories. – Divakar Feb 25 '14 at 12:23
  • @Divakar thanks that's perfect. It says nowhere on the docs page for `addpath` that it is temporary :/ could have saved me a lot of time if it did. `addpath(genpath('Spatial Econometrics'))` works perfectly. Feel free to add it as an answer. – Dan Feb 25 '14 at 12:28
  • 1
    I usually rename the folders to [package notation](http://www.mathworks.de/de/help/matlab/matlab_oop/scoping-classes-with-packages.html) for such cases. – bdecaf Feb 25 '14 at 14:11

2 Answers2

6

Add path to the top of MATLAB search paths for current MATLAB session only –

addpath(PATHNAME)

Same as addpath, but stays good for next sessions –

savepath(PATHNAME)

Add all subdirectories for current MATLAB session only -

addpath(genpath(PATHNAME))

Note: One must be careful while adding paths because if there are multiple function files with the same name, the one that is higher up on the path string, is chosen.

More info here – addpath, savepath, genpath.

Divakar
  • 218,885
  • 19
  • 262
  • 358
4

I use the following to keep my functions in a separate 'functions' folder in the same directory as the main script. As long as you know the path to the toolbox functions, this should work for you.

% Add path (at beginning of script)
added_path = [pwd,'/functions']; %change to: added_path = '/path' for your required path
addpath(added_path);

% Remove path (at end of script/script clean-up)
rmpath(added_path);

You may want to look at genpath() to get those long and windy toolbox paths in a manageable way.

The-Duck
  • 501
  • 5
  • 9