In previous lessons, you learned how to interact with MATLAB in the MATLAB command window by typing commands at the command prompt. And, you saw how you could define and use variables. However, let's say you have a sequence of commands which you find that you use over and over again, in many different MATLAB sessions. It would be nice if you didn't have to manually type these commands at the command prompt whenever you want to use them. This is one of the things that scripts can do for you. A MATLAB script is an ASCII text file (if you don't know what an ASCII text file is, read on) that contains a sequence of MATLAB commands; the commands contained in a script file can be run, in order, in the MATLAB command window simply by typing the name of the file at the command prompt. In this lesson, we will just be introducing scripts with simple examples; however, MATLAB is essentially a general-purpose programming language (like BASIC, C, Java, Pascal, or FORTRAN), and in their full generality scripts are programs written in the MATLAB language. So in learning to write scripts, you are basically learning computer programming!
You can use any text editor, such as Microsoft Windows Notepad, or word processor, such as Microsoft Word, to create scripts, but you must make sure that you save scripts as simple text documents (i.e., in the "Save As" dialogue box, choose "Text Document" or its equivalent for "Save as type:"). If you don't know what ASCII means, don't worry about it; basically it just means a simple text document (i.e., when you choose "Text Document" for "Save as type:" in the "Save As" dialogue box, you are saving as an ASCII text file). It will probably just be easiest to create your scripts using MATLAB's built-in text editor, which will be described later in this lesson and which automatically just saves files as ASCII text files for you. Also, when you name your script files, you will need to append the suffix ".m" to the filename, for example "my_script.m". Scripts in MATLAB are also called "m-files" because of this, and the ".m" suffix tells MATLAB that a file is a script (there is another use of m-files, to create your own functions, which you will learn about in a later lesson).
A Simple Script
So let's look at a simple example of a script that calculates the average of five numbers that are stored in variables. Here is the contents of a script file "average_script.m" that was created with MATLAB's built-in text editor:
%a simple MATLAB m-file to calculate the average of 5 numbers.
define variables for the 5 numbers:
%now calculate the average of these and print it out:
= (a + b + c + d + e) / 5;
You should already understand what the text in black does: it defines the five variables, calculates their average, and then prints out this average. The text in green (i.e., the lines starting with "%" --- all comment lines must start with "%") are comments. Comments are completely ignored by MATLAB (i.e., getting rid of any or all of the three comment lines above would not change what the script does at all), and are intended solely for you and others to read. They are a way for you to document important points about your scripts, so that, for example, if you put away some script for a long time and then come back to it later you can be quickly and easily reminded of what it is you were trying to do, and how to use the script; comments are also very useful in helping others to understand your scripts. You can also use comments in the MATLAB command window, but they are more often used in scripts. Some good advice: make liberal use of comments in your scripts. Note that another good reason to use comments is that the comments you place at the beginning of your scripts (called the header --- the header includes all comment lines up to the first command) will be returned to users when they get help for your script; thus, you should always try to place information about what your script is about and how to use it as comment lines at the beginning of your scripts. For example, if you place the following comment lines right at the beginning of a script called "my_script":
m-file which does ....
then this text would be returned to users who type "help my_script" at the command prompt (assuming the directory where "my_script" is located is in matlabpath).
If the above script "average_script.m" is saved in the present working directory, then it can be run simply by typing "average_script" at the command prompt in the MATLAB command window. Here is the result of running it:
Notice that the variable definitions for a, b, c, d, e, and five_number_average do not print out results; this is because they were followed by semicolons. Also notice that defined variables are still defined and can be used even after a script finishes running (the last "who" command above shows this).
MATLAB's Text Editor
Well, now that you know what scripts are and how to run them, let's talk about how you create them with MATLAB's built-in text editor. MATLAB's text editor is similar to other text editors, but it is especially suited for creating MATLAB-specific files, such as m-files. For example, it color codes different structural units of MATLAB scripts (e.g., as you saw in the above script which was created with MATLAB's built-in text editor, comments are green and command lines are black, and other constructs will use other colors) in order to organize the script for you, so, for example, you can find what you are looking for in the script more easily. In addition, it is important to note that MATLAB's text editor will automatically append the ".m" suffix to your script file for you, so you don't need to append it yourself (but you would have to append it yourself in other text editors, such as Windows Notepad). The MATLAB text editor can also be used for debugging (i.e., finding and fixing errors in scripts), but we won't talk about that here (the name of the text editor is, in fact, "MATLAB Editor/Debugger").
You can start MATLAB's text editor in two ways. One way is to simply type "edit" at the command prompt in the MATLAB command window. The other way is to select "New" from the "File" menu in the MATLAB command window; this will open up another menu, from which you should choose "M-file". These ways of starting MATLAB's text editor will start it up with a new, empty document; if you want to start the editor up with an existing file (e.g., a script file you have partially completed), then you can either type at the command prompt "edit" followed by the name of the file, or select "open" from the MATLAB command window's "file" menu (and then choose the file you want to open from the "open" dialoge box). The way you use MATLAB's text editor is essentially the same as how you would use Windows Notepad or other simple text editors (i.e., cutting and pasting, moving the cursor around the text, saving, etc. --- you should already know how to do basic text editing in Windows). In any case, the MATLAB Editor/Debugger window looks like this:
That is about all there is to it. Right now, you should practice using MATLAB's text editor. Go ahead and start MATLAB's text editor, and type in the following text for a very simple script
%This is my practice script:
define 2 variables:
let's assume a and b are the lengths of 2 sides of a right %triangle and
let's calculate the length of the other side (the %hypotenuse) using the
let's see the result:
After you type this text in, save it somewhere convenient (for example, if you have a thumb drive with you, save it in a directory on this solid state memory --- remember the complete path to wherever you saved it, though). After you save it, exit out of the text editor, and add the path to your script to matlabpath. (for example, if you saved your script on the thumb drive and this happens to have been assigned the drive letter “E:” then this script may end up being save as: "E:\my_scripts\practice_script.m" REMEMBER >>>> you would need to execute the command "addpath E:\my_scripts" at the command prompt). Then type the name of your script without the ".m" suffix at the command prompt (for example, "practice_script") and you should get the output:
You might now want to experiment some more with this practice script (for example, add more variables, try different functions such as sin, cos, etc.) To do that, just open your script up in MATLAB's text editor again, make your changes, save the file, exit the text editor, and finally type the name of the script at the command prompt. Have fun!
To continue on to the next lesson, click here.