Access to m files in a subfolder without constantly adding to the path

I have downloaded a toolbox with many files in many subfolders ( spatial-econometric tools ) for use in one specific project, and I don’t want to add it to the path, because I don’t think I'm going to use it, and I don’t know, will Does it hide functions in the statistics toolbar.

How do I access functions inside this toolkit? Is there a way, perhaps programmatically, to add it to the path only for the specific Matlab session that caused the script call? What is the right way to handle this?

+4
source share
3 answers

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

addpath(PATHNAME) 

Same as the add path, but suitable for the following sessions -

 savepath(PATHNAME) 

Add all subdirectories for current MATLAB session -

 addpath(genpath(PATHNAME)) 

Note. . You should be careful when adding paths, because if there are several function files with the same name, then that is higher on the path line.

More details here - addpath , savepath , genpath .

+6
source

I use the following to save my functions in a separate "functions" folder in the same directory as the main script. If you know the path to the toolbar 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 can look at genpath () to get these long and windy toolbar paths in a controlled way.

+2
source

Thanks @Divakar for the answer:

 addpath(genpath('Spatial Econometrics')) 

Adds a folder with all its subfolders to the path for this session only. It will not save the path until savepath is savepath .

+2
source

All Articles