addpath

Adds folders to the top of the search path.

Category: Programming > Files and Folders

Syntax

addpath(folder)

Puts folder at the front of the path.

addpath(folder1, ..., folderN)

Adds several folders; the first one ends up at the very front.

addpath(___, '-end')

Appends the folders at the end of the path instead.

oldpath = addpath(___)

Also returns the path as it was before the change.

Description

When a function name is used, the interpreter looks in the current folder first and then in each folder of the path, in order. addpath puts a folder in that list so that the .m files inside it can be called by name. A folder placed at the front shadows functions of the same name found later.

addpath does not search inside subfolders; combine it with genpath to add a whole tree. The change lasts for the session, and rmpath undoes it.

Input arguments

folder
Folder name as text, relative or absolute. Several folders can be given in one text separated by pathsep.
'-end'
Option that appends instead of prepending.

Output arguments

oldpath
The previous path, a character vector.

Example

Making a folder's functions callable

mkdir mylib
writelines('function y = triple(x), y = 3*x; end', 'mylib/triple.m');
exist('triple')
addpath('mylib')
exist('triple')
triple(4)
Output
ans = 0
ans = 2
ans = 12

exist returns 2 once the file is found on the path.

Notes

The path keeps the folder text as given, so a relative folder stays relative.

See also

rmpath path genpath exist which