Student Center
Creating Scripts with the MATLAB Editor/Debugger
- Prerequisites and Expectations for the Tutorial
- A Very Elementary MATLAB Tutorial
- Starting MATLAB
- Running the Demos
- Getting Help
- Creating Variables
- Performing Calculations
- Visualizing Data
- Creating Scripts with MATLAB Editor/Debugger
- Saving Variables and Sessions
- Working with Files, Directories and Paths
Introduction
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 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). So in learning to write scripts, you are basically learning computer programming!
You can use any text editor, such as Microsoft Windows Notepad, or wordprocessor, 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:"). 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 the file is associated with MATLAB.
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.% first define variables for the 5 numbers:
a = 5;
b = 10;
c = 15;
d = 20;
e = 25;
% now calculate the average of these and print it out:
five_number_average = (a + b + c + d + e) / 5;
five_number_averageYou 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 during execution, and are intended solely as comments to the user.
Comments are a way for you to document important points about your scripts, so for example, if you put away some script file 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.
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.
Running Scripts
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 MATLAB command prompt. Here is the result of running it:
clear
whos
pwd
ans =
D:\Applications\MATLAB6p5\work
dir
. .. average_script.m
average_script
five_number_average = 15
whos| Name | Size | Bytes | Class |
| a | 1x1 | 8 | array |
| ans | 1x30 | 60 | char array |
| b | 1x1 | 8 | double array |
| c | 1x1 | 8 | double array |
| d | 1x1 | 8 | double array |
| e | 1x1 | 8 | double array |
| five_number_average | 1x1 | 8 | double array |
Grand total is 36 elements using 108 bytes
Note:
1) If a MATLAB command is followed by a semicolon the output associated is suppressed.
2) The variables defined in the script remain in the workspace even after the script finishes running, as can be seen by the last whos command above.
The MATLAB Editor/Debugger
Well, now that you know what scripts are and how to run them, let's talk about how you create them with the MATLAB built-in text editor (the MATLAB Editor/Debugger). The MATLAB text editor is similar to other text editors, but it is especially suited for creating MATLAB-specific files, M-files.
There are a number of different features that the MATLAB text editor offer: One features that it offers is syntax highlighting (i.e., comments are green, command lines are black, and other constructs will use other colors), making easier to read the script. Another is that it automatically append the ".m" suffix when saving files, so you don't need to append it yourself. The MATLAB text editor also offers extensive debugging functionality as well (i.e., finding and fixing errors in scripts). In this tutorial we will not go into the debugging functionality of the MATLAB editor, but you can find examples in the Help Browser of MATLAB.
To start the MATLAB text editor simply type, edit, at the command prompt. Another way to invoke the editor is to select File->New->M-file from the MATLAB desktop menu bar. You should see the editor open with a new, empty documents. 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 File->Open… from the MATLAB Desktop, and then choose the file you want to open from the Open dialogue box. The way you use the MATLAB text editor is essentially the same as how you would use Windows Notepad, or other simple text editors. When opened the MATLAB Editor/Debugger window looks like this:
![]() |
| Click on image to see enlarged view. |
That is about all there is to it. Right now, you should practice using the MATLAB Editor/Debugger. Go ahead and start the MATLAB text editor. Below is an example that you can type in to get you started.
% This is my practice script:
% First define 2 variables:a = 3;
b = 4;% Then 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
% Pythagorean formula:
c = sqrt(a*a + b*b);
%and, let's see the result:
c
After you type this text in, save it in your current working directory (we will talk about directories and paths in a later lesson). 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:
practice_script
c = 5
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 the MATLAB Editor/Debugger again, make your changes, save the file, exit the text editor, and finally type the name of the script at the command prompt to run it. Have fun!
Continue on to the next lesson.
See also: Simulink Tutorial
Store
