addpath - Add folders to search path - MATLAB (original) (raw)

Add folders to search path

Syntax

Description

addpath([folderName1,...,folderNameN](#btpdm64-1-folderName1folderNameN)) adds the specified folders to the top of the search path for the current MATLAB® session.

If the input is a set of multiple folders separated by path separators, then each of the specified folders will be added.

example

addpath([folderName1,...,folderNameN](#btpdm64-1-folderName1folderNameN),[position](#btpdm64-1-position)) adds the specified folders to the top or bottom of the search path, as specified by position.

example

addpath(___,'-frozen') additionally disables folder change detection for the folders being added. When folder change detection is disabled for a folder, MATLAB does not detect changes made to the folder from outside of MATLAB.

Use this syntax with any of the arguments in previous syntaxes. You can specify '-frozen' and position in either order.

example

[oldpath](#btpdm64-1-oldpath) = addpath(___) additionally returns the path prior to adding the specified folders.

example

Examples

collapse all

Add Folder to Top of Search Path

Create a folder, add it to the top of your search path, and then save the search path for future MATLAB® sessions.

mkdir('matlab/myfiles')
addpath('matlab/myfiles')
savepath matlab/myfiles/pathdef.m

Add Folder to End of Search Path

Create the folder matlab/myfiles and add it to the end of the search path.

mkdir('matlab/myfiles') addpath('matlab/myfiles','-end')

Add Folder and Its Subfolders to Search Path

Add matlab/myfiles and its subfolders to the search path.

Create the folder matlab/myfiles and call genpath inside of addpath to add all subfolders of matlab/myfiles to the search path.

mkdir('matlab/myfiles') addpath(genpath('matlab/myfiles'))

Add Folder to Search Path and Disable Folder Change Notification

Create the folder matlab/myfiles. Then, add it to the top of the search path, disable folder change notification, and return the search path before adding the folder.

mkdir('matlab/myfiles') oldpath = addpath('matlab/myfiles','-frozen');

Disabling folder change notification is not supported in MATLAB® Online™.

Input Arguments

collapse all

folderName1,...,folderNameN — Folder names to add to search path

character vectors | string scalars

Folder names to add to the search path, specified as one or more character vectors or string scalars. Use the full path name for each folder. Usegenpath with addpath to add all subfolders of folderName.

Example: 'c:\matlab\work'

Example: '/home/user/matlab'

Example: '/home/user/matlab','/home/user/matlab/test'

MATLAB resolves all path names containing '.', '..', and symbolic links to their target location before adding them to the path. This ensures that each entry in the MATLAB path represents a unique folder location. For example, if you specify c:\matlab\..\work, MATLAB adds the folder c:\work to the path.

Data Types: char | string

position — Position on search path

'-begin' (default) | '-end'

Position on the search path, specified as one of the following:

Value of position Description
'-begin' Add specified folders to the top of the search path.
'-end' Add specified folders to the bottom of the search path.

Output Arguments

collapse all

oldpath — Path prior to addition of folders

character vector

Path prior to the addition of folders, returned as a character vector.

Tips

Algorithms

If you use addpath within a local function, the path change persists after program control returns from the function. That is, the scope of the path change is global.

Version History

Introduced before R2006a