RunMat
GitHub

savepath — Persist the current MATLAB search path to pathdef.m with status and diagnostic outputs.

savepath writes the current MATLAB search path to a pathdef.m file so that future sessions can restore the same ordering. The file is a MATLAB function that returns the path character vector, matching MathWorks MATLAB semantics.

How savepath works in RunMat

  • savepath() with no inputs writes to the default RunMat location ($HOME/.runmat/pathdef.m on Linux/macOS, %USERPROFILE%\.runmat\pathdef.m on Windows). The directory is created automatically when required.
  • savepath(file) writes to the specified file. Relative paths are resolved against the current working directory, ~ expands to the user's home folder, and supplying a directory (with or without a trailing separator) appends the standard pathdef.m filename automatically.
  • The function does not modify the in-memory search path - it only writes the current state to disk. Callers can therefore continue editing the path after saving without interference.
  • status = savepath(...) returns 0 on success and 1 when the file cannot be written. [status, message, messageID] = savepath(...) returns MATLAB-style diagnostics describing the failure. Both message outputs are empty on success.
  • Invalid argument types raise savepath: filename must be a character vector or string scalar. Empty filenames raise savepath: filename must not be empty.
  • When the RUNMAT_PATHDEF environment variable is set, the zero-argument form uses that override instead of the default location.

How savepath runs on the GPU

savepath runs entirely on the host. If callers supply a GPU-resident string, RunMat gathers it back to CPU memory before resolving the target path. No acceleration provider hooks or kernels are required.

GPU memory and residency

No. Because savepath interacts with the filesystem, GPU residency provides no benefit. The builtin automatically gathers GPU text inputs so existing scripts continue to work even if they accidentally construct filenames on the device.

Examples

Save The Current Search Path To The Default Location

status = savepath()

Expected output:

status =
     0

Persist A Project-Specific Pathdef File

status = savepath("config/project_pathdef.m")

Expected output:

status =
     0

Capture Status, Message, And Message ID

[status, message, messageID] = savepath("config/pathdef.m");
if status ~= 0
    warning("Failed to save the path: %s (%s)", message, messageID);
end

Append Genpath Output And Persist The Result

tooling = genpath("third_party/toolchain");
addpath(tooling, "-end");
savepath()

Save A Pathdef Using A Directory Argument

mkdir("~/.runmat/projectA");
savepath("~/.runmat/projectA/")

Expected output:

% Creates ~/.runmat/projectA/pathdef.m with the current search path.

Override The Target File With RUNMAT_PATHDEF

setenv("RUNMAT_PATHDEF", fullfile(tempdir, "pathdef-dev.m"));
savepath()

Expected output:

% The file tempdir/pathdef-dev.m now contains the MATLAB path definition.

Use gpuArray Inputs Transparently

status = savepath(gpuArray("pathdefs/pathdef_gpu.m"))

Expected output:

status =
     0

Inspect The Generated pathdef.m File

savepath("toolbox/pathdef.m");
type toolbox/pathdef.m

Expected output:

% Displays the MATLAB function that reproduces the saved search path.

FAQ

Where does savepath write by default?

RunMat uses $HOME/.runmat/pathdef.m (Linux/macOS) or %USERPROFILE%\.runmat\pathdef.m (Windows). Set RUNMAT_PATHDEF to override this location.

Does savepath create missing folders?

Yes. When the parent directory does not exist, RunMat creates it automatically before writing the file.

What happens if the file is read-only?

savepath returns status = 1 together with the diagnostic message and message ID RunMat:savepath:cannotWriteFile. The existing file is left untouched.

Does savepath modify the current path?

No. It only writes out the path. Use addpath, rmpath, or path to change the in-memory value.

Are argument types validated?

Yes. Inputs must be character vectors or string scalars. String arrays with multiple elements and numeric arrays raise an error.

Is the generated file MATLAB-compatible?

Yes. RunMat writes a MATLAB function named pathdef that returns the exact character vector stored by the path builtin, so MathWorks MATLAB and RunMat can both execute it.

How do I restore the path later?

Evaluate the generated pathdef.m (for example by calling run('~/pathdef.m')) and pass the returned value to path(). Future RunMat releases will load the default file automatically.

Can I store multiple path definitions?

Absolutely. Call savepath with different filenames for each profile, then run the desired file to switch.

Is savepath safe to call concurrently?

The builtin serializes through the filesystem. When multiple sessions write to the same path at once, the last write wins - this matches MATLAB's behavior.

Does savepath include the current folder (pwd)?

The file mirrors the output of the path builtin, which omits the implicit current folder exactly as MATLAB does.

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

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

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how savepath 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.