What does the savepath function do in MATLAB / RunMat?
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 does the savepath function behave in MATLAB / RunMat?
savepath()with no inputs writes to the default RunMat location ($HOME/.runmat/pathdef.mon Linux/macOS,%USERPROFILE%\.runmat\pathdef.mon 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 standardpathdef.mfilename 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(...)returns0on success and1when 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 raisesavepath: filename must not be empty. - When the
RUNMAT_PATHDEFenvironment variable is set, the zero-argument form uses that override instead of the default location.
savepath Function GPU Execution Behaviour
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 residency in RunMat (Do I need gpuArray?)
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 of using the savepath function in MATLAB / RunMat
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 behavior:
% 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 behavior:
% 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 behavior:
% Displays the MATLAB function that reproduces the saved search path.
FAQ
- Where does
savepathwrite by default? RunMat uses$HOME/.runmat/pathdef.m(Linux/macOS) or%USERPROFILE%\.runmat\pathdef.m(Windows). SetRUNMAT_PATHDEFto override this location. - Does
savepathcreate 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?
savepathreturnsstatus = 1together with the diagnostic message and message IDMATLAB:savepath:cannotWriteFile. The existing file is left untouched. - Does
savepathmodify the current path? No. It only writes out the path. Useaddpath,rmpath, orpathto 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
pathdefthat returns the exact character vector stored by thepathbuiltin, so MathWorks MATLAB and RunMat can both execute it. - How do I restore the path later? Evaluate the generated
pathdef.m(for example by callingrun('~/pathdef.m')) and pass the returned value topath(). Future RunMat releases will load the default file automatically. - Can I store multiple path definitions? Absolutely. Call
savepathwith different filenames for each profile, then run the desired file to switch. - Is
savepathsafe 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
savepathinclude the current folder (pwd)? The file mirrors the output of thepathbuiltin, which omits the implicit current folder exactly as MATLAB does.
See Also
path, addpath, rmpath, genpath
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/repl_fs/savepath.rs - Issues: Open a GitHub ticket with a minimal reproduction.