What does the tempdir function do in MATLAB / RunMat?
tempdir returns the absolute path of the operating system's temporary folder. The returned path always ends with the platform-specific file separator (/ on macOS and Linux, \ on Windows) so it is safe to concatenate file names directly, matching MATLAB behaviour.
How does the tempdir function behave in MATLAB / RunMat?
- The builtin accepts no input arguments; providing any arguments raises
tempdir: too many input arguments. - The output is a character row vector (
1×N) for compatibility with historical MATLAB code. Convert it withstring(tempdir)if you prefer string scalars. - The path reflects the process environment, honouring variables such as
TMPDIR(macOS/Linux) orTMP/TEMP(Windows). - When the OS reports a temporary folder that lacks a trailing separator, RunMat appends one automatically to mirror MATLAB.
- The directory is not created or cleaned automatically; RunMat relies on the operating system to manage the folder lifecycle.
- If RunMat cannot determine the temporary folder (rare), the builtin raises an error containing the operating system message.
tempdir Function GPU Execution Behaviour
tempdir performs no GPU work. It queries the host environment and surfaces the result as a character array. The builtin registers a CPU-only GPU spec so the fusion planner treats it as a host-side operation. Scripts that accidentally store paths on the GPU have nothing to worry about—tempdir has no inputs to gather.
GPU residency in RunMat (Do I need gpuArray?)
No. tempdir is always a host operation. There is nothing to gain by moving data to the GPU, and no acceleration provider needs to implement hooks for this builtin.
Examples of using the tempdir function in MATLAB / RunMat
Get The System Temporary Folder Path
folder = tempdir();
disp(folder);
Expected output (macOS/Linux):
/var/folders/xy/abcd/T/
Expected output (Windows):
C:\Users\alex\AppData\Local\Temp\
Combine tempdir With tempname To Build Unique Paths
uniqueFile = fullfile(tempdir(), "runmat-session.log");
fid = fopen(uniqueFile, "w");
fprintf(fid, "Temporary log created at %s\n", datetime);
fclose(fid);
Expected output:
% Creates runmat-session.log inside the system temporary folder
Create A Temporary Working Subfolder
workDir = fullfile(tempdir(), "mytask");
if ~exist(workDir, "dir")
mkdir(workDir);
end
disp(workDir);
Expected output:
% Prints the path to tempdir/mytask and creates the folder if needed
Verify That tempdir Appends A File Separator
folder = tempdir();
endsWithSeparator = folder(end) == filesep;
disp(endsWithSeparator);
Expected output:
1
Use tempdir When Creating Temporary Zip Archives
archive = fullfile(tempdir(), "results.zip");
zip(archive, "results");
disp(archive);
Expected output:
% Displays the full path to results.zip inside the system temp folder
Log Temporary Folder Usage For Debugging
fprintf("RunMat temp folder: %s\n", tempdir());
Expected output:
% Prints something like:
% RunMat temp folder: /tmp/
FAQ
Why does tempdir return a character vector instead of a string scalar?
MATLAB has historically returned character vectors. RunMat mirrors that behaviour for compatibility; wrap the result in string(...) when you need a string scalar.
Does the path include a trailing separator?
Yes. RunMat appends the platform-specific file separator when necessary so you can safely concatenate file names.
What happens if the temporary folder does not exist?
RunMat reports the folder reported by the operating system. Most platforms guarantee the folder exists; if not, subsequent calls such as mkdir can create it.
Can I change the temporary folder location?
Yes. Set TMPDIR (macOS/Linux) or TEMP/TMP (Windows) before starting RunMat. tempdir will honour those environment variables, just like MATLAB.
Will tempdir ever create or clean files automatically?
No. The builtin only reports the path. Cleanup policies remain the responsibility of the operating system or your code.
Is tempdir thread-safe?
Yes. Querying the temporary folder is read-only and does not modify global state.
Does tempdir work inside deployed or sandboxed environments?
As long as the process has permission to query the environment, it returns the best-effort location supplied by the OS.
Can I call tempdir on the GPU?
No. The function is host-only, but it also never transfers data to or from the GPU.
Does tempdir normalize path separators?
RunMat preserves the operating system’s separators (backslash on Windows, slash elsewhere) to maintain MATLAB compatibility.
What if I pass arguments to tempdir by mistake?
RunMat raises tempdir: too many input arguments, matching MATLAB’s diagnostic.
See Also
mkdir, rmdir, delete, dir, pwd
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/repl_fs/tempdir.rs - Issues: Open a GitHub ticket with a minimal reproduction.