Script m-files (last updated 9/9/99)
The information in this tutorial is located in Chapter 4 of the MATLAB manual, The Student Edition of MATLAB, Version 5, The MATH WORKS Inc. Prentice-Hall, 1997.
=======================================================================================
This tutorial includes the following sections;
Creating and
Executing a Script m-file
=======================================================================================
While MATLAB commands are relatively easy to type and execute, it can be tedious when a long list of commands needs to be executed several times, may need to be edited, or stored for later. Saving a MATLAB workspace only saves the values of the variables in memory, not any of the commands. MATLAB gives you a way to handle this by allowing a list of commands to be stored in a simple text file. You can then open the text file and MATLAB will execute the commands as if they were typed at the MATLAB prompt. These text files are called script files or m-files. The term m-file comes from the fact that the script files must have the '.m' extension, filename.m.
The commands in the m-file are executed from the workspace.
Any variables in the Command Window are available to the m-file and any
variables defined in the m-file will be stored in the Command Window memory
upon execution.
=======================================================================================
Creating and Executing a Script m-file
Creating a script m-file:
It is useful to type long arrays in an m-file because it is easy to edit if you make a mistake without retyping the entire array. You then just type the m-file name to put the array into memory.
There is no method available for directly saving a figure or graph. The method is to put all the commands into an m-file which can be saved. This is also very useful when there is a chance that the plot will need edited. When you use the gtext command to locate text on a graph it cannot be removed. If the command is in an m-file it can easily be edited or removed. (See the Plotting Basics Tutorial).
A number of commands for dealing with file management (listing, deleting, etc.) are shown in the table on page 37 of the MATLAB manual.