tempname — Generate unique temporary paths in the system temp directory or a specified target folder.
tempname generates a unique path string suitable for temporary files or folders. By default it uses the system temporary directory and supports MATLAB-compatible optional base-folder overrides.
Syntax
filename = tempname()
filename = tempname(folder)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
folder | StringScalar | Yes | — | Base folder used for generated temporary file paths. |
Returns
| Name | Type | Description |
|---|---|---|
filename | StringScalar | Unique temporary file path that does not currently exist. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | Folder argument cannot be resolved during home-directory expansion. | tempname: unable to resolve folder path |
| — | More than one positional argument is supplied. | tempname: too many input arguments |
| — | Folder argument is not a character vector, string scalar, or string-array scalar. | tempname: folder name must be a character vector or string scalar |
| — | Folder argument (or expanded folder path) is empty. | tempname: folder name must not be empty |
| — | System temporary directory cannot be determined. | tempname: unable to determine temporary directory |
| — | Unique temp name generation exhausted available attempts. | tempname: unable to generate a unique name |
How tempname works
tempname()returns a character row vector (1×N) containing an absolute path inside the system temporary directory.tempname(folder)returns a path insidefolder, which can be absolute or relative. RunMat expands leading~to the home directory for convenience.- The returned path never corresponds to an existing file or directory at the time of the call.
tempnamedoes not create files or directories. Use the returned value with builtins such asfopen,mkdir, ormovefile.- The generated token begins with the MATLAB-compatible
tpprefix followed by hexadecimal entropy, making it human-recognisable while avoiding collisions. - Providing more than one input argument raises
tempname: too many input arguments. Non-text inputs raise a descriptive type error.
Does RunMat run tempname on the GPU?
tempname performs all computation on the CPU. When scripts pass GPU-resident strings (for example, gpuArray("scratch")), RunMat automatically gathers those scalars to host memory before determining the result. Acceleration providers do not implement hooks for this builtin, and there is no GPU kernel to warm up.
GPU memory and residency
No. tempname only manipulates paths and never benefits from GPU execution. It always returns a host-resident character array. If you accidentally store a folder name on the GPU, RunMat gathers it transparently.
Examples
Generate A Unique Temporary File Name
fname = tempname();
fprintf("Saving intermediate results to %s\n", fname)Expected output:
% Prints the unique file name inside the system temporary folder.Create A Temporary File Name In A Custom Folder
logDir = fullfile(pwd(), "logs");
fname = tempname(logDir)Expected output:
% fname starts with the logs folder and ends with a tp******** token.Append A File Extension To Tempname Results
csvPath = [tempname(), ".csv"]Expected output:
% csvPath is a unique .csv file path you can pass to writematrix or fprintf.Reserve A Temporary Folder Path
scratch = tempname();
mkdir(scratch);
cleanupObj = onCleanup(@() rmdir(scratch, "s"))Expected output:
% scratch now exists on disk and is removed automatically via onCleanup.Use Tempname With fopen To Write Temporary Data
tmpFile = tempname();
[fid, message] = fopen(tmpFile, "w");
if fid == -1
error("Failed to open temp file: %s", message);
end
fprintf(fid, "Temporary output\n");
fclose(fid)Expected output:
% Creates a file, writes text, and leaves it for later processing.Combine Tempname With gpuArray Inputs
folder = gpuArray("scratch");
fname = tempname(folder)Expected output:
% RunMat gathers the string and returns a host-side character vector.Using tempname with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how tempname changes the result.
Run a small tempname example, explain the result, then change one input and compare the output.
FAQ
Does tempname create the file or folder for me?⌄
No. It only reserves a unique path. Call fopen, mkdir, or other functions to create the resource.
Can I call tempname with relative folders?⌄
Yes. RunMat honours relative paths and joins the generated token using the platform’s path separator.
What happens if the target folder does not exist yet?⌄
tempname still returns a path under that folder. It is up to your code to create intermediate directories if needed.
Why does the result start with tp?⌄
MATLAB prefixes the token with tp for temporary paths. RunMat follows the same convention for familiarity.
Is the result guaranteed to be unique?⌄
The builtin combines monotonic process-wide counters with high-resolution timestamps and the process ID. The result does not exist at the moment of generation; collisions are exceedingly unlikely.
Can I request multiple names at once?⌄
Call tempname repeatedly. Each invocation returns a fresh token.
Does tempname support Unicode folder names?⌄
Yes. Paths are stored as UTF-16 internally on Windows and UTF-8 on Unix-like systems. RunMat converts between encodings automatically.
How do I convert the result to a string scalar?⌄
Wrap the output in string(tempname()) or string(tempname(folder)).
Will GPU acceleration change the output?⌄
No. The builtin is host-only and ignores GPU providers entirely.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir
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 tempname is executed, line by line, in Rust.
- View the source for tempname 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.