Main Content

Save and Back Up Code

您可以保存您的文件在编辑器中,Edi生活tor using several methods. In the Editor, you also can create backup copies of your files. Creating backup copies of your files ensures that you have a known working version of the files before making changes to them, and can also be useful for recovering lost changes after a system problem.

Depending on your needs, you can also control how the files you save are encoded and cached.

Save Code

When you modify a file in the Editor or the Live Editor, MATLAB®indicates that there are unsaved changes in the file by displaying an asterisk (*) next to the file name in the document tab.

Unsaved file showing an asterisk next to the file name in the document tab.

To save the file, go to theEditororLive Editortab, and in theFilesection, clickSave.

To change the name, location, or type of a file, selectSave>Save As. For example, to save a live script as a plain code file (.m), on theLive Editortab, in theFilesection, selectSave>Save As. In the dialog box that appears, selectMATLAB Code files (UTF-8) (*.m)as theSave as typeand clickSave.

Back Up Code

You can create backup copies of your files in the Editor. Creating a backup copy of a file ensures that you have a known working version of the file before making changes to it. To create a backup copy of a file, on theEditortab, in theFilesection, selectSave>Save Copy As. This option is not available in the Live Editor or inMATLAB Online™.

In addition, when you modify files in the Editor, MATLAB automatically creates backup copies of the files. If you lose changes to your files due to system problems, you can use the automatically created backup copies of the files to recover the changes.

By default, MATLAB saves a backup copy of a modified file every five minutes using the same file name but with an.asvextension. For example,filename.mwould have a backup file name offilename.asv. If you lose changes to your file, you can recover the unsaved changes by opening the backup copy of the file,filename.asv, and saving it asfilename.m.

To change how and when MATLAB saves backup copies of files, on theHometab, in theEnvironmentsection, clickPreferences. Then, selectMATLAB>Editor/Debugger>Backup Files. You can specify:

  • How often to save backup copies of the files you are editing.

  • What file extension to use when creating backup copies of files.

  • Where to save backup copies of files.

  • Whether to automatically delete backup copies of files when you close the corresponding source file in the Editor.

For more information about the available options, see the Backup Files preferences inEditor/Debugger Preferences.

InMATLAB Online, every time you save a code file in the Editor, MATLAB stores the contents of your code file in the version history. For more information about recovering a previous version of a file inMATLAB Online, seeRestore Files in MATLAB Online.

MATLAB does not automatically create backups of files modified in the Live Editor.

Recommendations on Saving Files

MathWorks®recommends that you save files that you create to a folder outside thematlabrootfolder tree, wherematlabrootis the folder returned when you typematlabrootin the Command Window. Similarly, when you edit files that you get from MathWorks, save your edited version outside thematlabroot文件夹树。If you save your files in thematlabrootfolder tree, they can be overwritten when you install a new version of MATLAB.

If you do save files in thematlabrootfolder tree, you may need to take extra steps for your changes to take effect. At the beginning of each MATLAB session, MATLAB loads and caches in memory the locations of files in thematlabroot文件夹树。因此,如果您添加、删除或麦e changes to files in thematlabrootfolder tree using an external editor or file system operations, you must update the cache so that MATLAB recognizes the changes you made. For more information, seeToolbox Path Caching in MATLAB.

File Encoding

As of R2020a, when the Editor saves a new MATLAB code file that has a.mextension, such as a script or a function, it uses UTF-8 without a byte-order-mark (BOM). The Editor saves existing files with their current encoding unless a different one is selected from the Save As dialog. For example, to save a file using a legacy locale-specific encoding for compatibility with an earlier release of MATLAB, on theEditortab, in theFilesection, selectSave>Save as. In the dialog box that appears, select the desired encoding from theSave as typeoptions.

The current encoding is displayed next to the file name in the Editor status bar or, if the Editor Window is docked, the Desktop status bar.

Related Topics