Main Content

Change Folders on Search Path

You can programmatically and interactively add or remove folders and change the order of MATLAB® folders on the search path, for the current session and future sessions. When files with the same name appear in multiple folders on the search path, MATLAB uses the one in the folder nearest to the top of the search path.

Add or Remove Folders on the Search Path Programmatically

You can programmatically add one or more folders on the path using the addpath function. This example adds the folder to the top of the search path. See addpath for other options.

addpath("c:\matlab\MyFolder")

You can remove one or more folders from the path using the rmpath function.

rmpath("c:\matlab\MyFolder")

To save changes to the search path across MATLAB sessions, use the savepath function. This function also saves changes that you make interactively with the Set Path dialog box, from the Current Folder browser, or in the MATLAB Editor.

In MATLAB Online™, changes to the path are automatically saved. Therefore, calling savepath is not necessary.

Change Folders on Search Path Interactively

Use the Set Path dialog box to interactively make changes to the search path.

  1. On the Home tab, in the Environment section, click Set Path. The Set Path dialog box appears.

    Alternatively, you can access this dialog box using the pathtool function.

    Set Path dialog box showing several MATLAB search paths

  2. Use the Add Folder or Add Folder with Subfolders button to add new folders to MATLAB search path.

  3. Use the Move Down and Move Up buttons to change the order of files on the search path. Files contained in folders at the top of the search path have precedence over those in folders farther down. For more information, see Function Precedence Order.

  4. Apply or cancel the search path 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, and then Close.

    • To undo your changes, click Revert, and then Close.

    • To restore the default search path, click Default, and then Close. The default search path contains only folders provided by MathWorks®.

Note

The MATLAB (userpath) folder automatically moves to the top of the search path the next time you start MATLAB. For more information about the userpath folder, see userpath Folder on the Search Path.

In MATLAB Online, you can use the buttons at the top of the Set Path dialog box to change the folders on the search path. To search for folders on the path, use the search field at the top of the Set Path dialog box.

Change Folders Using Current Folder Browser

You can add or remove folders on the search path from the Current Folder browser. Folders that are not on the path appear transparent, while those on the path appear solid.

  1. From the Current Folder browser, right-click the folder or select and then right-click multiple folders to add or remove.

  2. From the context menu, select Add to Path or Remove from Path, and then select an option:

    • Selected Folders

    • Selected Folders and Subfolders

Context menu that appears when you right-click a folder in the Current Folder browser

Changes to the search path using this method do not persist in future MATLAB sessions. To save these changes, use savepath.

Change Folders on Search Path Using MATLAB Editor

In the MATLAB Editor, you also can add or remove the folder that contains a script on the search path. Right-click the document tab, and then select an option to add or remove the folder from the search path.

Context menu that appears when you right click a tab in the Editor

Changes to the search path using this method do not persist in future MATLAB sessions. To save these changes, use savepath.

See Also

| |

Related Topics