Automatically Save Backup M-Files with the MATLAB Editor

By Linda Chuss, MathWorks

The MATLAB Editor autosave feature was introduced in MATLAB 6.5 (Release 13). This feature automatically maintains backup files that preserve your M-file changes if MATLAB terminates unexpectedly, for example, due to a power failure. You can set autosave preferences that make it easy to remove the saved files when they are no longer needed.

These topics are addressed:

 Using Autosave Files Can Save You Time
After making changes to a file in the Editor/Debugger, if five minutes elapse and you have not saved your changes, the Editor/Debugger automatically saves a copy of the file for you. By default, the backup file is stored in the same location and with the same name as the original file, but has an .asv (for autosave) extension. For UNIX platforms, the default extension is appended with the tilde (~) character, making the autosave file filename.m~.

If you ever need to, you can easily recover your work from an autosave file. You might also want to recover an autosave file to access a version that does not contain changes that you recently made.

 Removing Unnecessary Autosave Files the Easy Way
While autosave files can help you recover from a disaster, they also can be a maintenance nuisance. Whenever you move a file to another directory, rename a file, or even delete a file, the original autosave version remains. You are left with a scattered collection of autosave files you will never need. One way to avoid this problem is to store all autosave files in a single directory that you specify. Then, on a regular basis, you can remove all autosave files at once by deleting the contents of that directory. Chances are that if you have not used an autosave version during a session, you will not need it later.

You use preferences to specify an autosave directory for all autosave files. To do so, follow these four steps.
Step 1: Select File -> Preferences -> Editor/Debugger -> Autosave.
Step 2: Under Location, select Single Directory and then browse to or supply the name of the directory where all autosave files will be stored. Click OK.

Figure 1 – Autosave Preferences for the Editor/Debugger. Click on image to see enlarged view.

Step 3: When you no longer need the autosave files, select all the files in the autosave files directory and delete them.
Step 4: To make the autosave file deletion process even easier, you can include a line in your finish.m file to clean out the autosave directory every time you quit MATLAB. A finish.m file is a script you create to run specified functions when MATLAB shuts down.

For example, add this to your finish.m file:

For more information, see finish in the online documentation at the MathWorks Web site.

The single directory preference is also useful if you edit files that are in read-only directories. MATLAB cannot store the autosave version in the directory with the original file because the directory is read-only. But MATLAB can save the autosave version to the single autosave directory you specify in preferences.

Additional Autosave Preferences
Other autosave preferences allow you to:

  • Turn autosave off
  • Specify the amount of time before the Editor makes an
    automatic save
  • Specify a different extension for autosave files
  • Save untitled files, that is, new files that you have not yet saved and are therefore untitled

For more information, see Preferences for the Editor/Debugger in the online documentation at the MathWorks Web site.

Remember to set up the autosave preferences to accommodate your work environment so that you can take full advantage of the autosave feature in the MATLAB Editor.

Published 2003

Products Used

Learn More