MATLAB: What does addpath do and why should I use it when I am compiling a code in matlab

compilerMATLAB Compilermatlab function

Hi All
I am compiliing a code in MATLAB and am advised to use addpaht
what does it do and how does it help me in compiling ?
addpath(genpath([pwd,'\SubFunctions\']))

Best Answer

genpath() when passed the name of a directory, creates a ":" separated list of that directory and all sub-directories of it that are potentially valid MATLAB paths. That excludes directories whose name starts with @ or + or is the word private (or, in sufficiently new MATLAB releases) or is the word resources
Once the list of directories and subdirectories is created, it is passed to addpath(), which adds the list (by default) to the beginning of the list of directories that is the MATLAB "path".
The MATLAB "path" is the list of directories that is to be searched to find .m and .p and .mex* and .slx and .mld and similar executable files that might be invoked when MATLAB is asked to execute a function. Generally MATLAB starts at the first entry in the list and checks it to see if function matches any executable file in there, then goes on to the second directory in the list, and so on -- so earlier entries in the list generally get priority over later entries. This is not always the case, due to some complications involving classes and packages.
The MATLAB "path" is also the list of directories to be searched to find any data file that is requested to be read in, such as an image file or audio file or .mat file. You can, for example, ask to imshow('cameraman.tif') and MATLAB will search along the path until it finds cameraman.tif to use.
The interactive version of MATLAB adjusts the path at runtime as the code uses cd , But at compile time, MATLAB does not execute the code, so any code that might cd into the SubFunctions directory to be sure to execute the functions there, cannot be taken into account at compile time. The work-around to that is to add to the path all directories that might be cd'd to for that purpose.
And sometimes there are cases when building a large software project where functions that need to be compiled in are not the same as you would use interactively -- for example functions to work around the fact that some functions cannot be compiled.
In short, the addpath is being done to give information to the compiler about which directories to search for functions it might encounter during compilation.