Development Environment |
This section covers the following topics:
Purpose of the Search Path
MATLAB uses a search path to find M-files and other MATLAB related files, which are organized in directories on your file system. These files and directories are provided with MATLAB and associated toolboxes. Any file you want to run in MATLAB must reside in a directory that is on the search path or in the current directory. By default, the files supplied with MATLAB and MathWorks toolboxes are included in the search path.
If you create any MATLAB related files, add the directories containing the files to the MATLAB search path. For instructions to view and modify the search path, see Viewing and Setting the Search Path.
Note
Save any M-files you create and any MathWorks-supplied M-files that you edit in a directory that is not in the $matlabroot/toolbox directory tree. If you keep your files in $matlabroot/toolbox directories, they can be overwritten when you install a new version of MATLAB. Also note that locations of files in the $matlabroot/toolbox directory tree are loaded and cached in memory at the beginning of each MATLAB session to improve performance. If you save files to $matlabroot/toolbox directories using an external editor or add or remove in from these directories using file system operations, run rehash toolbox before you use the files in the current session. If you make changes to existing files in $matlabroot/toolbox directories using an external editor, run clear functionname before you use the files in the current session. For more information, see rehash or Toolbox Path Caching. |
How the Search Path Works
The search path is also referred to as the MATLAB path. Files included are considered to be on the path. When you include a directory on the search path, you add it to the path. Subdirectories must be explicitly added to the path; they are not on the path just because their parent directories are. The search path is stored in the file pathdef.m
.
The order of directories on the path is relevant. MATLAB looks for a named element, for example, foo
, as described here. If you enter foo
at the MATLAB prompt, MATLAB performs the following actions:
foo
as a variable.
foo
as a built-in function.
foo.m
.
foo.m
.
Although the actual search rules are more complicated because of the restricted scope of private functions, subfunctions, and object-oriented functions, this simplified perspective is accurate for the ordinary M-files you usually work with.
The order of the directories on the search path is important if there is more than one function with the same name. When MATLAB looks for that function, only the first one in the search path order is found. Other functions with the same name are considered to be shadowed and cannot be executed. For more information, see How MATLAB Determines Which Method to Call in Programming and Data Types.
To see the pathname used, use which
for a specified function. For more information, see the reference page for which
.
You can use a different pathdef.m
if you store it in your startup directory--see Startup Directory for MATLAB.
Preferences for the Array Editor | Viewing and Setting the Search Path |