Desktop Tools and Development Environment Previous page   Next Page

Search Path

This section covers the following topics:

About 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. By default, the files supplied with MATLAB and MathWorks products are included in the search path. These are all of the directories and files under $matlabroot/toolbox.

Any file you want to run in MATLAB must reside in a directory that is on the search path, or in the current directory. If you create any MATLAB related files, add the directories containing the files to the MATLAB search path. For instructions to view the search path and add directories to it, see Viewing and Setting the Search Path, including Caution Against Saving Files in $matlabroot/toolbox.

The search path is also referred to as the MATLAB path. Directories included are considered to be on the path. When you include a directory in 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.

Adding directories to the path is similar to performing an include or import in some other applications.

How the Search Path Determines Which Function to Use

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. Looks in the current directory for a file named foo.m.
  3. Searches the directories on the MATLAB search path, in order, for foo as a built-in function, followed by foo.m which is not built-in.

If there is more than one function with the same name, the order of directories on the path determines which of those functions MATLAB uses. When MATLAB looks for that function, it uses the first one found in the search path:

Although the actual search path rules are more complicated because of the restricted scope of private functions, subfunctions, object-oriented functions, P-files, and MAT-files, this simplified perspective is accurate for the ordinary M-files you usually work with. For more information, see Determining Which Function Is Called in the MATLAB Programming documentation.


Previous page  Preferences for the Array Editor How MATLAB Finds the Search Path, pathdef.m Next page

© 1994-2005 The MathWorks, Inc.