0

I have a MATLAB script that needs to write temporary files to disk, and then delete these files when I'm done with them. It's important that I delete the files myself (rather than letting them accumulate in the temp directory) because the code in question is in a loop that executes many times. If the files aren't promptly deleted, the script will quickly gobble up large amounts of disk space.

I thought that the code snippet below would work, and it does when I run it on its own. For some reason, though, the snippet fails when I include it in my larger script. The temporary files don't get deleted and clutter up the temp directory.

    % create temporary file
    myTmpFile = [tempname '.wav'];
    fs = 32000;
    wavwrite(zeros(fs,1),fs,myTmpFile);
    

    % use the file here
 

    % delete the file when we're done
    prevState = recycle('off'); % turn recycle off to permanently delete files
    delete(myTmpFile)
    recycle(prevState);         % restore the state of recycle

Does anyone have any idea what I'm doing wrong? I'm running MATLAB 7.10.0 on OS X 10.7.3.

1
  • 1
    You may be interested in defining a onCleanup object so that the file will get deleted even if the function exits due to an error.
    – Jonas
    May 5, 2012 at 10:11

3 Answers 3

2

You should check out the tempdir and tempname functions that ship as part of MATLAB. They're designed to provide absolute paths to file names in a directory where you're guaranteed to have write access. See the documentation for both functions at the MathWorks web site.

Based on the problems you're seeing I suspect (as does nrz) that you're writing your temporary files into whatever happens to be MATLAB's current directory. Not only does that cause you to lose track for the files for deletion purposes later, but some day you'll wind up in a read-only directory and be completely out of luck!

1
  • Hi Bob -- whoops, I actually made a typo somehow when I copied this snippet into StackOverflow. tempfile should have been tempname. (I've edited the question.) So, actually, I was using the standard MATLAB functions for generating the temp files, and was still getting the behaviour described. I've figured out the problem though -- it turned out to be unrelated to the snippet above. Anyway, thanks for the answer!
    – dB'
    May 5, 2012 at 3:33
1

Are you using relative or absolute filename? If you change current folder with cd inside the loop or if any of the functions your code calls calls cd without returning to the original folder afterwards, relative filenames won't work. You can check current folder with pwd.

Possible solutions: 1. Avoid cd. 2. Use absolute filenames.

1
  • hi nrz -- I was actually doing all of this already, but it's still sound advice, so thank you for sharing it!
    – dB'
    May 5, 2012 at 3:43
1

I figured it out. It turns out the problem wasn't in the snippet above; it was in a completely different section of my code. I was writing temp files elsewhere and had forgotten to delete them.

Bottom line: there's absolutely nothing wrong with the snippet above. tempname, recycle and delete are perfectly good functions for creating and destroying temporary files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.