View all functions

CategoryIo: Repl Fs

What does the genpath function do in MATLAB / RunMat?

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 does the genpath function behave in MATLAB / 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.

genpath Function GPU Execution Behaviour

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 residency in RunMat (Do I need gpuArray?)

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

Examples of using the genpath function in MATLAB / RunMat

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.
  • Will symbolic links be traversed? 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.

See Also

addpath, rmpath, path, which

Source & Feedback