RunMat
GitHub

tempname — Return a unique temporary file path in the system temp folder or a user-specified directory.

tempname returns a unique path that you can reserve for a temporary file or folder. By default it chooses the system temporary directory. When you provide a folder argument, the path is generated inside that folder instead.

How tempname works in RunMat

  • tempname() returns a character row vector (1×N) containing an absolute path inside the system temporary directory.
  • tempname(folder) returns a path inside folder, 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.
  • tempname does not create files or directories. Use the returned value with builtins such as fopen, mkdir, or movefile.
  • The generated token begins with the MATLAB-compatible tp prefix 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.

How tempname runs 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.

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.

These functions work well alongside tempname. Each page has runnable examples you can try in the browser.

tempdir, mkdir, fopen, delete, movefile, addpath, cd, copyfile, dir, exist, fullfile, genpath, getenv, ls, path, pwd, rmdir, rmpath, savepath, setenv

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how tempname works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.