RunMat
GitHub

genpath — Generate a MATLAB-style search path string for a folder tree.

genpath walks a folder tree and returns a character vector that lists the folder and every subfolder in depth-first order. Each entry is separated by the platform pathsep character (: on Linux/macOS, ; on Windows), making the result a drop-in argument for addpath, rmpath, and other path-editing utilities.

How genpath works in RunMat

  • genpath() with no inputs uses the current working directory (pwd) as the root folder. genpath(folder) accepts either an absolute or relative path.
  • MATLAB-reserved directories named private or resources, as well as class folders (@MyClass) and namespace folders (+pkg), are skipped automatically along with all of their descendants. The root folder is still included even if it matches one of these reserved names.
  • All returned entries are absolute, canonicalised paths beginning with the root directory, followed by each descendant directory in lexical order. The walking order matches MATLAB's depth-first behaviour.
  • Duplicate directories are removed automatically. Symlinked folders that resolve to the same physical location only appear once in the output.
  • genpath(folder, excludes) omits any folder listed in excludes together with its descendants. The excludes argument is a character vector of absolute or relative paths separated by pathsep. Relative entries are resolved against the supplied root folder before canonicalisation.
  • If a folder in the tree cannot be read (for example due to permissions), RunMat silently skips that branch while keeping the rest of the result intact.
  • If the requested root folder does not exist, RunMat raises genpath: folder '<name>' not found, matching MATLAB error semantics.

How genpath runs on the GPU

genpath operates entirely on the host filesystem. Any string inputs that reside on the GPU are gathered back to the CPU before processing. No acceleration provider hooks are invoked and no device kernels are launched.

GPU memory and residency

No. genpath manipulates host-side strings and filesystem metadata. Supplying gpuArray values provides no benefit—RunMat gathers the data automatically.

Examples

Generate a recursive path for the current working folder

p = genpath();
addpath(p);   % Add the current folder and all subfolders to the MATLAB path

Build a search path for a project toolbox

toolboxRoot = "toolbox/signal";
toolboxPath = genpath(toolboxRoot);
addpath(toolboxPath, "-end")

Exclude build output directories from the generated path

root = "projects/solver";
excludes = strjoin([
    fullfile(root, "build"),
    fullfile(root, "dist")
], pathsep);
p = genpath(root, excludes)

Skip a private utilities folder while including the rest of the tree

root = "analysis";
p = genpath(root, fullfile(root, "private"))

Automatically skip MATLAB-reserved folders

root = "toolbox";
mkdir(root, "private");
mkdir(root, "+package");
p = genpath(root)

Expected output:

% `p` only includes `toolbox` and subfolders that are not named `private` or
% `resources`, and that do not begin with `@` or `+`.

Combine genpath with savepath for persistent tooling setup

p = genpath("third_party/toolchain");
addpath(p);
savepath()

FAQ

Does genpath include package (+pkg) and class (@Class) folders?

No. These directories are excluded automatically, along with folders named private or resources. Use the excludes argument for additional rules.

How do I exclude multiple folders?

Build a character vector or string using pathsep (for example, strjoin({path1, path2}, pathsep)) and pass it as the second argument. Relative entries are interpreted relative to the root folder.

Why are some folders missing from the result?

RunMat only omits folders that you explicitly exclude or that cannot be read due to filesystem permissions.

What happens if the root folder does not exist?

genpath raises genpath: folder '<name>' not found, matching MATLAB. Create the folder first or adjust the argument.

Can I call genpath on a GPU array?

Yes—the input is gathered to the CPU before processing, and the result is returned as a standard character vector.

Is the result always absolute paths?

Yes. RunMat resolves the root and all discovered folders to their absolute, canonical locations to avoid duplicates.

Yes, but the target directory only appears once in the output. Directory cycles are prevented by tracking canonical paths.

Can I feed genpath output directly into addpath or rmpath?

Absolutely. The output string is pathsep-delimited specifically for that purpose.

These functions work well alongside genpath. Each page has runnable examples you can try in the browser.

addpath, rmpath, path, which, cd, copyfile, delete, dir, exist, fullfile, getenv, ls, mkdir, movefile, pwd, rmdir, savepath, setenv, tempdir, tempname

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how genpath works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.