| MATLAB® | ![]() |
| On this page… |
|---|
Adding a Directory to the Search Path Handles to Functions Not on the Path Making Toolbox File Changes Visible to MATLAB® |
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:
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 Fundamentals documentation.
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:
For more information: See Multiple Implementation Types in the MATLAB Programming Fundamentals documentation.
To add a directory to the search path, use either of the following:
At the toolbar, select File > Set Path.
At the command line, use the addpath function.
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:
addpath(genpath('K:/toolbox/control'))
For more information: See Search Path in the MATLAB Desktop Tools and Development Environment documentation.
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,
Create a script in this off-path directory that constructs function handles and assigns them to variables. That script might look something like this:
File E:/testdir/createFhandles.m fhset = @setItems fhsort = @sortItems fhdel = @deleteItem
Run the script from your current directory to create the function handles:
run E:/testdir/createFhandles
You can now execute one of the functions by means of its handle.
fhset(item, value)
Unlike functions in user-supplied directories, M-files (and MEX-files) in the matlabroot/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 matlabroot/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.
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.
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:
help changeNotification help changeNotificationAdvanced
![]() | Evaluating Expressions | Program Control | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |