Programming and Data Types    


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. Built-in function
  3. Subfunction
  4. Private function
  5. Class constructor
  6. Overloaded method
  7. M-file in the current directory
  8. M-file on the path

If the name is found to be an M-File on the path (No. 8 in the list), and there is more than one M-File on the path with the same name, MATLAB uses the one in the directory that is closest to the beginning of the path string.

For more information: See Function Precedence Order in the MATLAB "Programming and Data Types" 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 Selecting Methods from Multiple Implementation Types in the MATLAB "Programming and Data Types" 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 "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 through its handle using feval.

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:

  Evaluating Expressions Program Control