Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Using the Search Path

What Is the Search Path?

The search path is:

MATLAB can access all files in the folders on the search path.

What Is on the Search Path?

Class, package, and private folders are not on the search path. See Files and Folders MATLAB Can Access.

Order of Folders on the Search Path

The order of folders on the search path is relevant when two files with the same name are in folders on the search path. MATLAB uses the file nearer to the top of the search path. See Ensuring MATLAB Uses the File You Want.

How MATLAB Stores the Search Path

When you change the search path, MATLAB uses it in the current session. To use it in future sessions, save the changes.

MATLAB saves the search path information in the pathdef.m file. The pathdef.m file is a series of full path names, one for each folder on the search path, separated by a semicolon (;).

By default, pathdef.m is in matlabroot/toolbox/local.

Ways to View and Change the Search Path

Using the Set Path Dialog Box

To view and change the MATLAB search path using the Set Path dialog box:

  1. Open the dialog box by selecting File > Set Path.

    The Set Path dialog box opens, listing all folders on the search path.

    Image of Set Path dialog box showing key features.

  2. Add, remove, and change the order of folders on the search path as described in the following topics:

  3. Commit or cancel changes:

See also Using the Search Path.

Adding Folders to the Search Path

To add folders to the search path, you can use the Current Folder browser, the Set Path dialog box, or functions.

Adding Folders to the Search Path from the Current Folder Browser

  1. Right-click the folders you want to add.

  2. From the context menu, select Add to Path, and select the option you want:

    • Containing Folder (parent folder of the selected files or folders)

    • Selected Folders

    • Selected Folders and Subfolders

MATLAB adds the specified folders to the top of the search path.

Adding Folders to the Search Path Using the Set Path Dialog Box

  1. Open the dialog box by selecting File > Set Path.

  2. Select an option:

    • Add Folder

    • Add with Subfolders

  3. In the resulting Browse for Folder dialog box:

    1. Select the folder you want to add to the search path.

    2. Click OK.

    MATLAB adds the specified folder to the top of the search path.

  4. Apply the changes:

    • To use the newly modified search path only in the current session, click Close.

    • To reuse the newly modified search path in the current session and future sessions, click Save.

  5. Click Close.

Adding Folders to the Search Path Using Functions

Use the addpath function.

Removing Folders from the Search Path

To remove folders from the search path, you can use the Set Path dialog box or functions.

Removing Folders from the Search Path Using the Set Path Dialog Box

  1. Select File > Set Path.

  2. In the resulting dialog box, select the folders you want to remove from the search path.

  3. Click Remove.

  4. Apply the changes:

    • To use the newly modified search path only in the current session, click Close.

    • To reuse the newly modified search path in the current session and future sessions, click Save.

  5. Click Close.

Removing Folders from the Search Path Using Functions

Use the rmpath function.

Changing the Order of Folders on the Search Path

To change the order of folders on the search path, you can use the Set Path dialog box.

To move folders to the top or bottom of the search path, you can use functions.

Changing the Order of Folders on the Search Path Using the Set Path Dialog Box

  1. Select File > Set Path.

  2. In the resulting dialog box, select the folders you want to move.

  3. Click one of the Move buttons, such as Move to Top. The order of the folders changes.

  4. To use the newly modified search path in future sessions, click Save.

    If you do not save the changes, the newly modified search path remains in effect until you end the current session of MATLAB.

  5. Click Close.

Moving a Folder to the Top or Bottom of the Search Path Using Functions

Use the path function.

Saving Changes to the Search Path

Changes you make to the search path remain in effect during the current session of MATLAB. For MATLAB to use the changed search path in subsequent sessions, save the search path, which updates the pathdef.m file.

Ways to Save Changes to the Search Path

Where to Save the Search Path File

Save the search path to the default location, matlabroot/toolbox/local, so MATLAB can locate it.

If you do not have write access to the default location, MATLAB prompts you for a different location. Choose the MATLAB startup folder.

Restoring the Default Search Path

The default search path contains only folders provided by The MathWorks.

Ways to restore the default search path:

See also Recovering from Problems with the Search Path.

Using the Search Path with Different MATLAB Installations

Using the Search Path with Different Versions

The default search path changes for each release of MATLAB because the default folders that come with the products change. Different versions of MATLAB cannot use the same pathdef.m file.

To use your files with a new version of MATLAB or with multiple versions, do one of the following:

Using the Search Path with Different Platforms

To use your files with MATLAB on different platforms, include addpath statements in the startup.m file. See Specifying Startup Options Using the Startup File for the MATLAB Program, startup.m

Recovering from Problems with the Search Path

When there is a problem with the search path, you cannot use MATLAB successfully.

You could experience search path problems when:

For example, when you start MATLAB, if a message like the following appears, it indicates a problem with the search path:

Warning: MATLAB did not appear to successfully set the search 
path...

To recover from problems with the search path, try the following steps, in order, proceeding to the next step only as necessary:

  1. Ensure MATLAB is using the pathdef.m file you expect:

    1. Run

      which pathdef
    2. If you want MATLAB to use the pathdef.m file at a different location, make corrections. For example, delete the incorrect pathdef.m and ensure the correct pathdef.m is in a location that MATLAB can access. See Where to Save the Search Path File.

  2. Look for and correct problems with the pathdef.m and startup.m files:

    1. Open pathdef.m and startup.m in a text editor. Depending on the problem, you might not be able to open the pathdef.m file.

    2. Look for obvious problems, such as invalid characters or path names.

    3. Make corrections and save the files.

    4. Start MATLAB again to ensure that the problem does not recur.

  3. Try to correct the problem using the Set Path dialog box:

    1. Restore the default search path and save it. See Using the Set Path Dialog Box. Depending on the problem, you might not be able to open the dialog box.

    2. Start MATLAB again to ensure that the problem does not recur.

  4. Restore the default search path using functions:

    1. Run restoredefaultpath, which sets the search path to the default and stores it in matlabroot/toolbox/local.

    2. If restoredefaultpath seems to correct the problem, run savepath.

    3. Start MATLAB again to ensure that the problem does not recur.

    Depending on the problem, a message such as the following could appear:

    The path may be bad. Please save your work (if desired), and quit.
  5. Correct the search path problems encountered during startup:

    1. Run

      restoredefaultpath; matlabrc
      

      Wait a few minutes until it completes.

    2. If there is a pathdef.m in the startup folder, it caused the problem. Either remove the bad pathdef.m or replace it with a good pathdef.m file. For example, run:

      savepath('path_to_your_startup_folder/pathdef.m')

      See Startup Folder for the MATLAB Program.

    3. Start MATLAB again to ensure that the problem does not recur.

After correcting problems with the search path, make any changes to run your files. For example, add the userpath folder or other folders to the search path.

Making Changes to Folders on the Search Path

You could encounter errors or unexpected behavior when you try to delete, rename, or move folders that:

The behavior varies by platform because it depends on the behavior of similar features in the operating system.

If your task fails and the error message indicates it is because the folder is on the search path, do one of the following:

  1. Remove the folder from the search path.

  2. Delete, rename, or move the folder.

  3. Add the folder to the search path.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS