| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
The search path is:
A subset of all the folders in the file system.
One way that MATLAB efficiently locates files used with MathWorks products.
Also referred to as the path.
MATLAB can access all files in the folders on the search path.
By default, folders provided with MATLAB and other MathWorks products. Relevant folders under matlabroot/toolbox are:
By default, MATLAB, the userpath folder. See Locations for Storing Your Files.
Folders you add to the search path, for the files you and other users create.
Adding folders to the search path is like performing an include or import operation in other applications.
Class, package, and private folders are not on the search path. See Files and Folders MATLAB Can Access.
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.
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.
Use the path and other functions in the Search Path category.
Specifying Startup Options Using the Startup File for the MATLAB Program, startup.m
To view and change the MATLAB search path using the Set Path dialog box:
Open the dialog box by selecting File > Set Path.
The Set Path dialog box opens, listing all folders on the search path.

Add, remove, and change the order of folders on the search path as described in the following topics:
Commit or cancel changes:
Keep the changes for use in the current session by clicking Close.
Keep changes for use in the current and future sessions by clicking Save. See Saving Changes to the Search Path.
Undo changes by clicking Revert.
Restore the default search path by clicking Default. See Restoring the Default Search Path.
See also Using the Search Path.
To add folders to the search path, you can use the Current Folder browser, the Set Path dialog box, or functions.
Right-click the folders you want to add.
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.
Open the dialog box by selecting File > Set Path.
Add Folder
Add with Subfolders
In the resulting Browse for Folder dialog box:
Select the folder you want to add to the search path.
Click OK.
MATLAB adds the specified folder to the top of the search path.
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.
Use the addpath function.
To remove folders from the search path, you can use the Set Path dialog box or functions.
Select File > Set Path.
In the resulting dialog box, select the folders you want to remove from the search path.
Click Remove.
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.
Click Close.
Use the rmpath function.
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.
Select File > Set Path.
In the resulting dialog box, select the folders you want to move.
Click one of the Move buttons, such as Move to Top. The order of the folders changes.
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.
Note The MATLAB (userpath) folder automatically moves to the top of search path the next time you start MATLAB. See Locations for Storing Your Files. |
Use the path function.
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.
Click Save in the Set Path dialog box. See Using the Set Path Dialog Box.
Use savepath.
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.
The default search path contains only folders provided by The MathWorks.
Ways to restore the default search path:
Click Default in the Set Path dialog box. See Using the Set Path Dialog Box. This method also adds the userpath folder—see Locations for Storing Your Files.
Use the restoredefaultpath function.
See also Recovering from Problems with the Search Path.
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:
For each version, add the folders containing your files to the search path. Save the search path where that version of MATLAB can access it.
Instead of changing the pathdef.m file, include addpath statements in the startup.m file. Use the same startup.m file with the multiple versions of MATLAB.
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
When there is a problem with the search path, you cannot use MATLAB successfully.
You could experience search path problems when:
You save the search path on a Windows platform, and then try to use the same pathdef.m file on a Linux platform.
The pathdef.m file becomes corrupt, invalid, renamed, or deleted.
MATLAB cannot locate the pathdef.m file.
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:
Ensure MATLAB is using the pathdef.m file you expect:
Run
which pathdef
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.
Look for and correct problems with the pathdef.m and startup.m files:
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.
Look for obvious problems, such as invalid characters or path names.
Make corrections and save the files.
Start MATLAB again to ensure that the problem does not recur.
Try to correct the problem using the Set Path dialog box:
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.
Start MATLAB again to ensure that the problem does not recur.
Restore the default search path using functions:
Run restoredefaultpath, which sets the search path to the default and stores it in matlabroot/toolbox/local.
If restoredefaultpath seems to correct the problem, run savepath.
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.
Correct the search path problems encountered during startup:
Run
restoredefaultpath; matlabrc
Wait a few minutes until it completes.
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')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.
You could encounter errors or unexpected behavior when you try to delete, rename, or move folders that:
Are on the search path
Contain subfolders that are on the search path
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:
Remove the folder from the search path.
Delete, rename, or move the folder.
Add the folder to the search path.
![]() | Determining and Changing the Current Folder | Related Topics for Managing Files | ![]() |

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 |