How to Save a Formula or Command as a Script in MATLAB

By Jim Sizemore, John Paul Mueller

After you create a formula or command that you want to use to perform a number of calculations in MATLAB, be sure to save it to disk. Of course, you can save anything that you want to disk. The following steps help you save any formula or command that you want to disk so that you can review it later:

1Choose a location to save the formula or command in the Address field.

Right-click the formula or command that you want to save in the Command History window.

2Choose Create Script from the context menu.

You see the Editor window. The script is currently untitled, so you see the script name as Untitled*.

If you want to select multiple commands to place in a script, you can choose them by clicking on the first command, and then using Ctrl+Click to select any additional commands. Each time you Ctrl+Click on a command, MATLAB highlights its entry. The commands will appear in the script file in the same order in which they appear in the Command History window.

3Click Save on the Editor tab.

You see the Select File for Save As dialog box.

4In the left pane, highlight the location you want to use to save the file.

You will see a File Name field.

5Type a name for the script in the File Name field.

The example uses FirstScript.m. However, when you save your own scripts, you should use a name that will help you remember the content of the file. Descriptive names are easy to remember and make precisely locating the script you want much easier later.

MATLAB filenames can contain only letters and numbers. You can’t use spaces in a MATLAB filename. However, you can use the underscore in place of a space.

6Click Save.

MATLAB saves the script for you so that you can reuse it later. The title bar changes to show the script name and its location on disk.

7Close the Editor window.

The Current Folder window displays the folder and script file.