Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access m-files in a subfolder without permanently adding it to the path

Tags:

matlab

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?

like image 550
Dan Avatar asked Dec 26 '22 12:12

Dan


2 Answers

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.

like image 61
Divakar Avatar answered Dec 28 '22 23:12

Divakar


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.

like image 35
The-Duck Avatar answered Dec 29 '22 00:12

The-Duck