tempdir — Return the system temporary directory path with a trailing platform file separator.
tempdir returns the absolute path of the operating system temporary directory. It follows MATLAB-compatible trailing-separator behavior so file names can be concatenated safely.
Syntax
folder = tempdir()Returns
| Name | Type | Description |
|---|---|---|
folder | StringScalar | Absolute path to the system temporary directory with trailing separator. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | Any positional input argument is supplied. | tempdir: too many input arguments |
| — | OS temporary directory resolution returns an empty path. | tempdir: unable to determine temporary directory (OS returned empty path) |
How tempdir works
- 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.
Does RunMat run tempdir on the GPU?
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 memory and residency
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
Get The System Temporary Folder Path
folder = tempdir();
disp(folder)Expected output:
/tmp/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 folderCreate 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 neededVerify That tempdir Appends A File Separator
folder = tempdir();
endsWithSeparator = folder(end) == filesep;
disp(endsWithSeparator)Expected output:
1Use 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 folderLog Temporary Folder Usage For Debugging
fprintf("RunMat temp folder: %s\n", tempdir())Expected output:
% Prints something like:
% RunMat temp folder: /tmp/Using tempdir with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how tempdir changes the result.
Run a small tempdir example, explain the result, then change one input and compare the output.
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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempname
Tabular
csvread · csvwrite · dlmread · dlmwrite · readmatrix · writematrix
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Json
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how tempdir is executed, line by line, in Rust.
- View the source for tempdir 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.