How to Revise and Call Scripts in MATLAB

By Jim Sizemore, John Paul Mueller

Scripts usually aren’t perfect the first time you write them in MATLAB. Editing them quite a few times is common. Even if the script does happen to attain perfection, eventually you want to add features, which means revising the script. The point is, you commonly see your scripts in the Editor window more than once. Here are some techniques you can use to open a script file for editing:

  • Double-click the script’s filename in the Current Folder window.

  • Click the down arrow on the Open option of the Home tab of the MATLAB window and select the file from the list. (The list will contain every kind of file you have recently opened, not just script files.)

  • Click the down arrow on the Open option of the Editor tab of the Editor window and select the file from the list. (The list will include only the most recently used script files.)

  • Click Find Files in the Editor tab of the Editor window to display the Find Files dialog box. Enter a search criteria, such as *.m (where the asterisk is a wild-card character for all files) and click Find. Double-click the file you want to open in the resulting list.

  • Locate the file using your platform’s hard drive application (such as Windows Explorer in Windows or Finder on the Mac) and double-click the file entry.

It’s a really bad idea to make changes to a script and then try to use it without testing it first. Always test your changes to ensure that they work as you intend them to. Otherwise, a change that you thought would work, could cause data damage or other problems.

Creating scripts without having some way to run them would be pointless. Fortunately, MATLAB lets you use scripts in all sorts of ways. The act of using a script — causing it to run — is known as calling the script. You can call scripts in these ways:

  • Right-click the script file and select Run from the context menu that appears.

  • Select the script file and press F9.

  • Type the filename on the command line and press Enter. (Adding the extension isn’t necessary.)

  • Type the script filename in another script.

The last method of calling a script is the most important. It enables you to create small pieces of code (scripts) and call those scripts to create larger, more powerful, and more useful pieces of code. The next step is creating functions that can send information in and out of those smaller pieces of code.