Programming Previous page   Next Page

MATLAB Path

This section covers the following topics:

Precedence Rules

When MATLAB is given a name to interpret, it determines its usage by checking the name against each of the entities listed below, and in the order shown:

  1. Variable
  2. Subfunction
  3. Private function
  4. Class constructor
  5. Overloaded method
  6. M-file in the current directory
  7. M-file on the path, or MATLAB built-in function

Regarding the last item on this list, all built-in functions have a .bi file on the path that is used internally by MATLAB. If you have an M-file on the path that has the same name as a MATLAB built-in, MATLAB selects the function that has its M- or .bi file in the directory closest to the beginning of the path string.

If you have two or more M-files on the path that have the same name, MATLAB selects the function that has its M-file in the directory closest to the beginning of the path string.

For more information: See Function Precedence Order in the MATLAB Programming documentation.

File Precedence

If you refer to a file by its filename only (leaving out the file extension), and there is more than one file of this name in the directory, MATLAB selects the file to use according to the following precedence:

  1. MEX-file
  2. MDL-file (Simulink® model)
  3. P-Code file
  4. M-file

For more information: See Multiple Implementation Types in the MATLAB Programming documentation.

Adding a Directory to the Search Path

To add a directory to the search path, use either of the following:

You can also add a directory and all of its subdirectories in one operation by either of these means. To do this from the command line, use genpath together with addpath. The online help for the genpath function shows how to do this.

This example adds /control and all of its subdirectories to the MATLAB path:

For more information: See Search Path in the MATLAB Desktop Tools and Development Environment documentation.

Handles to Functions Not on the Path

You cannot create function handles to functions that are not on the MATLAB path. But you can achieve essentially the same thing by creating the handles through a script file placed in the same off-path directory as the functions. If you then run the script, using run path/script, you will have created the handles that you need.

For example,

  1. Create a script in this off-path directory that constructs function handles and assigns them to variables. That script might look something like this:
  2. Run the script from your current directory to create the function handles:
  3. You can now execute one of the functions by means of its handle.

Making Toolbox File Changes Visible to MATLAB

Unlike functions in user-supplied directories, M-files (and MEX-files) in the $MATLAB/toolbox directories are not time-stamp checked, so MATLAB does not automatically see changes to them. If you modify one of these files, and then rerun it, you may find that the behavior does not reflect the changes that you made. This is most likely because MATLAB is still using the previously loaded version of the file.

To force MATLAB to reload a function from disk, you need to explicitly clear the function from memory using clear functionname. Note that there are rare cases where clear will not have the desired effect, (for example, if the file is locked, or if it is a class constructor and objects of the given class exist in memory).

Similarly, MATLAB does not automatically detect the presence of new files in $MATLAB/toolbox directories. If you add (or remove) files from these directories, use rehash toolbox to force MATLAB to see your changes. Note that if you use the MATLAB Editor to create files, these steps are unnecessary, as the Editor automatically informs MATLAB of such changes.

Making Nontoolbox File Changes Visible to MATLAB

For M-files outside of the toolbox directories, MATLAB sees the changes made to these files by comparing timestamps and reloads any file that has changed the next time you execute the corresponding function.

If MATLAB does not see the changes you make to one of these files, try clearing the old copy of the function from memory using clear functionname. You can verify that MATLAB has cleared the function using inmem to list all functions currently loaded into memory.

Change Notification on Windows

If MATLAB, running on Windows, is unable to see new files or changes you have made to an existing file, the problem may be related to operating system change notification handles.

Type the following for more information:


Previous page  Evaluating Expressions Program Control Next page

© 1994-2005 The MathWorks, Inc.