Instructions on how to add a directory to your search path
Last updated 2020-07-20 by Adam Lu
In MATLAB, to be able to call a custom script/function, one of the following must be true:
- It exists in the current directory.
- The directory containing it exists in the search path for your MATLAB session.
There are two main methods one can add a directory to the search path.
Method 1: Add it to the search path for whenever your code is called (Recommended)
-
In your code, apply the
addpath
command:addpath('/home/Matlab/Adams_Functions') addpath('/home/Matlab/Downloaded_Functions/ViolinPlot')
-
If
/home/Matlab/Adams_Functions
is within your search path by default, you could add the following code block from function_template.m:% Add path for custom functions only if not compiled if ~isdeployed % Locate the functions directory functionsDirectory = locate_functionsdir; % Add path for custom functions addpath_custom(fullfile(functionsDirectory, 'Adams_Functions')); addpath_custom(fullfile(functionsDirectory, 'Downloaded_Functions')); end
-
Alternatively, you could add the following code block
% Add path for custom functions only if not compiled if ~isdeployed if exist(fullfile(pwd, 'Adams_Functions'), 'dir') == 7 functionsdirectory = pwd; elseif exist('/home/Matlab/', 'dir') == 7 functionsDirectory = '/home/Matlab/'; elseif exist('/scratch/al4ng/Matlab/', 'dir') == 7 functionsDirectory = '/scratch/al4ng/Matlab/'; else error('Valid functionsDirectory does not exist!'); end addpath(fullfile(functionsDirectory, 'Adams_Functions')); end
-
Explanation:
- If the code is to be compiled in the future,
addpath()
would not work when compiled. One can check whether the code was compiled withisdeployed
- If you want the code to work across servers, including Rivanna, you can check for existence of the shared
Matlab
folder. addpath_custom.m
(available in/home/Matlab/Adams_Functions
) is superior overaddpath()
because it will not add a path if it already exists. This would prevent the search path from becoming too long when a function is repeatedly called.
- If the code is to be compiled in the future,
Method 2: Add it to the search path for all future MATLAB sessions called from your home directory
-
Click on
Set Path
and interactively add the directory containing the script/function.Note: If you use the
Set Path
interface, the optionAdd With Subfolders
is NOT recommended unless you are absolutely sure the contents of the subfolders do not interfere with the built-in functions or the more updated versions of your functions. - The search path can be saved as a file
pathdef.m
in your home directory. If so, the next time you open MATLAB from a terminal (which defaults to your home directory), the saved search path will be restored.pathdef.m
can be saved in any directory, but one must start thematlab
command from that directory to load the file.- If you start MATLAB from a directory without
pathdef.m
, only the default paths will be loaded.
-
If MATLAB has been upgraded, earlier versions of
pathdef.m
may cause errors when called. In this case, remove the file with the command:# In a terminal rm ~/pathdef.m