genpath — Generate recursive path strings from folder trees in MATLAB and RunMat.
genpath(folder) walks a folder tree and returns a path string containing the folder and subfolders in depth-first order. Path separator and traversal behavior match MATLAB semantics.
Syntax
pathstr = genpath()
pathstr = genpath(folder)
pathstr = genpath(folder, excludes)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
folder | StringScalar | Yes | — | Root folder to traverse recursively. |
excludes | StringScalar | Yes | — | Path-list string of folders to exclude. |
Returns
| Name | Type | Description |
|---|---|---|
pathstr | StringScalar | Generated recursive path string. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | The folder argument is not a character vector, string scalar/array scalar, or tensor of character codes. | genpath: folder must be a character vector or string scalar |
| — | The excludes argument is not a character vector, string scalar/array scalar, or tensor of character codes. | genpath: excludes must be a character vector or string scalar |
| — | More than two positional arguments are provided. | genpath: too many input arguments |
| — | Current directory cannot be resolved while locating the traversal root. | genpath: unable to resolve current directory |
| — | Requested folder path does not exist. | genpath: folder not found |
How genpath works
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
privateorresources, 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 inexcludestogether with its descendants. Theexcludesargument is a character vector of absolute or relative paths separated bypathsep. 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.
Does RunMat run genpath 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 pathBuild 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()Using genpath with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how genpath changes the result.
Run a small genpath example, explain the result, then change one input and compare the output.
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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how genpath is executed, line by line, in Rust.
- View the source for genpath 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.