Using MATLAB Script Files

In the last tutorial post, we learned about using MATLAB as a calculator to do some simple math operations. In this post, we will discuss a more powerful tool: the MATLAB script file!

A MATLAB script file is a text file containing a series of MATLAB commands. You can identify MATLAB script files by their .m file extension. When you run (or in programming lingo - execute) a script file, it carries out the commands sequentially just as if you typed them individually at the command prompt. This makes script files convenient for creating projects, programs, and saving your work. In fact, employing script files (and a later topic: MATLAB functions) is the standard way you will use MATLAB.

Creating MATLAB Script Files

You can create a MATLAB script file by clicking on the New Script icon in the MATLAB toolbar as shown below. You may open an existing script file by navigating through the "Open" menu or by simply double clicking on the name of the script file you wish to edit in the working directory (refer back to the MATLAB environment post if you don't remember these areas). In the figure, you could edit scriptFile.m by double clicking it or also by typing edit scriptFile.m in the command window.

Basic MATLAB Tutorial - Creating and Opening a Script File

After creating or opening a new script, you are presented with a new window: the MATLAB Editor. In the editor you can specify a list of mathematical commands just as in the command window; however, they will not be performed until the script file is run.

It is a good habit to save your script file as soon as you create it. To do this, simply click on the Save icon in the editor window and give your file a descriptive name. It is important to note that MATLAB script file names cannot contain special characters other than the underscore (_). File names also must begin with a letter. Two of the most common errors I see for new MATLAB users is trying to save files with names beginning with numbers or containing periods (for example, 2014code.m and homework.4.m are NOT valid file names). In addition, you should avoid naming your file with the same name as an existing MATLAB function. For example, plot.m. Plot is a built in command in MATLAB so if you name your script file that, you will no longer be able to use the plot command!

Running a Script File

Once you have saved your new script file and entered some commands, you can run/execute your program by clicking on the Run icon in the editor window toolbar (other ways to run a script are to press F5 on the keyboard when you are in the editor window or to enter the name of the script file at the command prompt). MATLAB will execute each command in the script line-by-line (imagine a typewriter or printer producing a page).

Basic Matlab Tutorial - MATLAB Script Editor

Output that is produced by the script will appear in the MATLAB command window as shown below. Notice that output can be suppressed from the command window by ending a line in the script file with a semicolon (;). The result of any line in the script with a semicolon will be hidden while that of lines without will show up in the command window. This can be seen by comparing the contents of the script file in the picture above with the resulting command window output in the picture below.

Basic MATLAB Tutorial - Script File Command Window Output


Comments are an important part of any program as they allow us to make notes inside of our programs or script files. In MATLAB, a user comment is specified using the percentage symbol (%). In a script file, any text that follows a % is ignored by MATLAB when it is run. You can see that in the example script file above I used comments to describe what my script file is doing at various steps.

With a basic understanding of how to use script files to make basic MATLAB programs, lets move to the next tutorial topic: MATLAB Vectors and Scalar Variables.

icon-long-arrow-left Last icon-cog Next icon-long-arrow-right

Leave a Reply

Your email address will not be published. Required fields are marked *