Development Environment    

Search Path

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.

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:

  1. Looks for foo as a variable.
  2. Checks for foo as a built-in function.
  3. Looks in the current directory for a file named foo.m.
  4. Searches the directories on the MATLAB search path, in order, for 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