addpath — Add folders to the MATLAB/RunMat search path for function and script resolution.
addpath prepends or appends folders to the active search path used to resolve functions, scripts, classes, and data files. Changes apply immediately to the current session, with path ordering behavior matching MATLAB semantics.
Syntax
oldpath = addpath(folder1)
oldpath = addpath(folder1, folder2, ...)
oldpath = addpath(folder1, ..., position)
oldpath = addpath(folder1, ..., "-frozen")
oldpath = addpath(folder1, ..., position, "-frozen")Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
folder1 | Any | Yes | — | Folder, path-list string, or container of folders to add. |
folder1 | Any | Yes | — | First folder argument. |
folderN | Any | Variadic | — | Additional folder arguments. |
folders | Any | Variadic | — | Additional folder arguments. |
position | StringScalar | Yes | "-begin" | Insertion position flag: "-begin" or "-end". |
frozen | StringScalar | Yes | "-frozen" | Compatibility flag accepted but currently ignored. |
Returns
| Name | Type | Description |
|---|---|---|
oldpath | StringScalar | Previous search path string. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | Folder arguments are not character vectors, string scalars/arrays, tensors of character codes, or cell arrays containing those forms. | addpath: folder names must be character vectors, string scalars, string arrays, or cell arrays of character vectors |
| — | No folder arguments are provided, or all provided folder tokens are empty/options only. | addpath: at least one folder must be specified |
| — | A position option is repeated or multiple position options are provided. | addpath: position option must be '-begin' or '-end' and may only appear once |
| — | A pathdef token is provided; loading pathdef.m is not implemented. | addpath: loading pathdef.m is not implemented yet |
| — | Current directory cannot be resolved while normalizing a relative folder. | addpath: unable to resolve current directory |
| — | A requested folder path does not exist. | addpath: folder not found |
| — | A requested path exists but is not a directory. | addpath: path is not a folder |
How addpath works
- Folder arguments may be character vectors, string scalars, string arrays, or cell arrays of character vectors or strings. Multi-row char arrays contribute one folder per row (trailing padding is stripped).
- Multiple folders can be passed in a single argument using the platform path separator (
:on Linux/macOS,;on Windows); this is compatible withgenpath. - By default, folders are added to the top of the search path. Use
'-end'to append or'-begin'to force prepending explicitly. Only one position flag is permitted. - The
'-frozen'flag is accepted for MATLAB compatibility. RunMat does not currently track frozen entries separately; the flag simply suppresses incompatibility warnings. - Duplicate entries are removed automatically so each folder appears at most once. On Windows, comparisons are case-insensitive.
- Inputs are resolved to absolute, canonicalised paths. Relative inputs are interpreted relative to the current working directory, and
~expands to the user’s home directory. - Folders must exist. RunMat raises
addpath: folder '<name>' not foundwhen a directory is missing oraddpath: '<name>' is not a folderwhen the target is not a directory.
Does RunMat run addpath on the GPU?
addpath manipulates host-side configuration. If any input value resides on the GPU, RunMat gathers it back to the host before parsing. No acceleration provider hooks or kernels are involved.
GPU memory and residency
No. addpath operates entirely on CPU-side strings. Supplying gpuArray text inputs offers no benefit—RunMat gathers them automatically.
Examples
Add a single folder to the top of the search path
mkdir("util/toolbox");
oldPath = path();
addpath("util/toolbox");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/util/toolboxAppend folders to the end of the search path
mkdir("shared/filters");
mkdir("shared/signal");
oldPath = path();
dirs = ["shared/filters", "shared/signal"];
addpath(dirs, "-end");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/shared/filters:/shared/signalUse genpath output to add a folder tree
mkdir("third_party/toolchain");
mkdir("third_party/toolchain/bin");
oldPath = path();
toolchain = genpath("third_party/toolchain");
addpath(toolchain);
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/third_party/toolchain:/third_party/toolchain/binAdd folders from a cell array
mkdir("src/algorithms");
mkdir("src/visualization");
oldPath = path();
folders = ["src/algorithms", "src/visualization"];
addpath(folders, "-begin");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/src/algorithms:/src/visualizationMove an existing folder to the top of the search path
mkdir("src/algorithms");
oldPath = path();
addpath("src/algorithms");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/src/algorithmsAccept the MATLAB -frozen flag
mkdir("vendor/hardware");
oldPath = path();
addpath("vendor/hardware", "-frozen");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/vendor/hardwareRetrieve the previous path and restore it later
mkdir("analysis/utilities");
oldPath = path();
old = addpath("analysis/utilities");
newPath = path();
path(old);
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/analysis/utilitiesCombine multiple options
mkdir("contrib");
mkdir("docs/examples");
oldPath = path();
addpath("contrib", "docs/examples", "-end", "-frozen");
newPath = path();
count = fprintf('%s %s\n', oldPath, newPath);Expected output:
/contrib:/docs/examplesUsing addpath with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how addpath changes the result.
Run a small addpath example, explain the result, then change one input and compare the output.
FAQ
Does addpath insist on absolute paths?⌄
No. Relative inputs are resolved against the current working directory and stored as absolute paths.
What happens with duplicate folders?⌄
Existing occurrences are removed before the new ordering is applied, so each folder appears only once.
How do I append instead of prepend?⌄
Supply '-end' as the final argument. Use '-begin' to force prepending explicitly.
Is -frozen supported?⌄
The flag is accepted for MATLAB compatibility. RunMat currently treats it as a no-op but plans to integrate tighter tooling once savepath support lands.
Can I load pathdef.m directly?⌄
Not yet. RunMat will add parity support in a future release. For now, evaluate the file manually and pass the resulting character vector to path.
Do folders need to exist?⌄
Yes. RunMat validates that every entry exists and is a directory before updating the search path.
Will addpath accept GPU strings?⌄
Yes. Inputs are gathered automatically, then processed on the CPU.
Does addpath return the new path?⌄
Like MATLAB, addpath returns the previous path so it can be restored later with path(old).
Related Io functions
Repl Fs
cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · savepath · setenv · tempdir · tempname
Tabular
csvread · csvwrite · dlmread · dlmwrite · readmatrix · writematrix
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Json
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how addpath is executed, line by line, in Rust.
- View the source for addpath in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.