What does the tempname function do in MATLAB / RunMat?
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 does the tempname function behave in MATLAB / RunMat?
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.
tempname Function GPU Execution Behaviour
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 residency in RunMat (Do I need gpuArray?)
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 of using the tempname function in MATLAB / RunMat
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.
See Also
tempdir, mkdir, fopen, delete, movefile
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/io/repl_fs/tempname.rs - Issues: Open a GitHub ticket with a minimal reproduction.