What does the pwd function do in MATLAB / RunMat?
pwd returns the absolute path of the folder where RunMat is executing. The result is a character row vector (1×N) so existing MATLAB code that expects traditional character output keeps working.
How does the pwd function behave in MATLAB / RunMat?
- Always returns the current working folder of the RunMat process.
- Output is a character vector using the platform-native path separators.
- Does not accept any input arguments; if arguments are provided, MATLAB raises an error. RunMat follows the same rule by reporting
pwd: too many input arguments. - Errors if RunMat is unable to query the current folder from the operating system.
- Designed to cooperate with
cdso workflows likestart = pwd; cd("subfolder"); ...; cd(start);behave exactly as they do in MATLAB.
pwd Function GPU Execution Behavior
pwd never runs on the GPU. It performs a host-side query of the process working directory and returns the result as a character vector. The builtin registers a CPU-only GPU spec with ResidencyPolicy::GatherImmediately, ensuring fusion plans always surface the path on the host. Because there are no inputs, nothing is gathered from device memory and acceleration providers do not need to implement any hooks for this builtin.
GPU residency in RunMat (Do I need gpuArray?)
No. pwd always operates on the CPU. Even in scripts that perform extensive GPU computation, you can call pwd at any time to confirm the working folder without affecting GPU residency or triggering device transfers.
Examples of using the pwd function in MATLAB / RunMat
Show The Current Working Folder
current = pwd;
disp(current);
Expected output (example on macOS/Linux):
/Users/alex/runmat-project
Expected output (example on Windows):
C:\Users\alex\runmat-project
Capture The Folder Before Changing Directories
startDir = pwd;
if ~exist("results", "dir")
mkdir("results");
end
cd("results");
% ... produce artifacts in results/ ...
cd(startDir);
Expected output:
% Restores the original folder after work completes
Combine pwd With cd To Print Relative Paths
old = cd("..");
fprintf("Now working in: %s\n", pwd);
cd(old);
Expected output:
% Prints the absolute path to the parent directory, then returns to the old folder
% For example:
% Now working in: /Users/alex
Confirm The Folder Inside Scripts
fprintf("Script started in %s\n", pwd);
Expected output:
% Example output:
% Script started in /Users/alex/runmat-project
Log The Working Folder Together With Results
logFile = "run.log";
fid = fopen(logFile, "w");
fprintf(fid, "Working folder: %s\n", pwd);
fclose(fid);
Expected output:
% The log file contains a line such as:
% Working folder: /Users/alex/runmat-project
Handle Errors When The Working Folder Is Unavailable
try
location = pwd;
catch err
disp(err.message);
end
Expected output:
% Displays a descriptive error message if RunMat cannot query the folder,
% for example:
% pwd: unable to determine current directory (Permission denied)
FAQ
- Why does
pwdreturn a character vector instead of a string? MATLAB historically returns character vectors forpwd. RunMat mirrors that behavior so existing code keeps working. Usestring(pwd)if you prefer string scalars. - Does
pwdreflect changes made withcd? Yes. Any successfulcdcall immediately affects whatpwdreturns, matching MATLAB's semantics. - Can
pwdfail? It is rare, but the operating system can prevent the process from querying the current folder. In that case RunMat raises an error that includes the OS reason. - Does
pwdnormalize the path? RunMat returns the operating-system path exactly as reported, just like MATLAB. - Is
pwdsafe to call from GPU-heavy scripts? Absolutely. The builtin does not allocate GPU memory or trigger device operations.
See Also
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/repl_fs/pwd.rs - Found an issue? Open a GitHub ticket with a minimal reproduction.