Main Content

Create Temporary Files

Use the tempdir function to return the name of the folder designated to hold temporary files on your system. For example, issuing tempdir on The Open Group UNIX® systems returns the /tmp folder.

Use the tempname function to return a file name in the temporary folder. The returned file name is a suitable destination for temporary data. For example, if you need to store some data in a temporary file, then you might issue the following command first:

fileID = fopen(tempname,'w');

In most cases, tempname generates a universally unique identifier (UUID). However, if you run MATLAB® without JVM®, then tempname generates a random name using the CPU counter and time, and this name is not guaranteed to be unique.

Some systems delete temporary files every time you reboot the system. On other systems, designating a file as temporary means only that the file is not backed up.