![]() |
jely fish |
of this approach is that you must be careful when naming files. When
MATLAB searches for files, it uses the first file with the correct name that it
finds in the path list, starting with the current working directory. If you use
the same name for different files in different directories in your path, you can
run into problems.
You can also control the MATLAB search path from the Path Browser.
To open the Path Browser, type editpath or pathtool, or select File:Set
Path.... The Path Browser consists of a panel, with a list of directories in the
current path, and several buttons. To add a directory to the path list, click
on Add Folder... or Add with Subfolders..., depending on whether or not
you want subdirectories to be included as well. To remove a directory, click on
Remove. The buttons Move Up and Move Down can be used to reorder the
directories in the path. Note that you can use the Current Directory browser to
examine the files in the working directory, and even to create subdirectories,
move M-files around, etc.
The information displayed in the main areas of the Path Browser can also be
obtained from the command line. To see the current working directory, type
pwd. To list the files in the working directory type either ls or dir. To see
the current path list that MATLAB will search for files, type path.
If you have many toolboxes installed, path searches can be slow, especially
with lookfor. Removing the toolboxes you are not currently using from the
MATLAB pathis one way to speed up execution.
Using the Command Window
We have already described in Chapters 1 and 2 how to enter commands in the
MATLAB Command Window. We continue that description here, presenting
an example that will serve as an introduction to our discussion of M-files.
Suppose you want to calculate the values of
sin(0.1)/0.1, sin(0.01)/0.01, and sin(0.001)/0.001
to 15 digits. Sucha simple problem can be worked directly in the Command
Window. Here is a typical first try at a solution, together with the response
that MATLAB displays in the Command Window:
>> x = [0.1, 0.01, 0.001];
>> y = sin(x)./x
36 Chapter 3: Interacting with MATLAB
y =
0.9983 1.0000 1.0000
After completing a calculation, you will often realize that the result is not
what you intended. The commands above displayed only 5 digits, not 15. To
display 15 digits, you need to type the command format long and then
repeat the line that defines y. In this case you could simply retype the latter
line, but in general retyping is time consuming and error prone, especially for
complicated problems. How can you modify a sequence of commands without
retyping them?
For simple problems, you can take advantage of the command history feature
of MATLAB. Use the UP- and DOWN-ARROW keys to scroll through the list
of commands that you have used recently. When you locate the correct command
line, you can use the LEFT- and RIGHT-ARROW keys to move around in the
command line, deleting and inserting changes as necessary, and then press
the ENTER key to tell MATLAB to evaluate the modified command. You can
also copy and paste previous command lines from the Command Window, or
in the MATLAB 6 Desktop from the Command History window as described
earlier in this chapter. For more complicated problems, however, it is better to
use M-files.
M-Files
For complicated problems, the simple editing tools provided by the Command
Window and its history mechanism are insufficient. A much better approach
is to create an M-file. There are two different kinds of M-files: script M-files
and function M-files. We shall illustrate the use of both types of M-files as we
present different solutions to the problem described above.
M-files are ordinary text files containing MATLAB commands. You can create
and modify them using any text editor or word processor that is capable of
saving files as plain ASCII text. (Suchtext editors include notepad in Windows
or emacs, textedit, and vi in UNIX.) More conveniently, you can use
the built-in Editor/Debugger, which you can start by typing edit, either by
itself (to edit a new file) or followed by the name of an existing M-file in the
current working directory. You can also use the File menu or the two leftmost
buttons on the tool bar to start the Editor/Debugger, either to create a new
file or to open an existing file. Double-clicking on an M-file in the Current
Directory browser will also open it in the Editor/Debugger.
M-Files 37
Script M-Files
We now show how to construct a script M-file to solve the mathematical problem
described earlier. Create a file containing the following lines:
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
We will assume that you have saved this file with the name task1.m in your
working directory, or in some directory on your path. You can name the file
any way you like (subject to the usual naming restrictions on your operating
system), but the “.m” suffix is mandatory.
You can tell MATLAB to run (or execute) this script by typing task1 in
the Command Window. (You must not type the “.m” extension here; MATLAB
automatically adds it when searching for M-files.) The output — but not the
commands that produce them — will be displayed in the Command Window.
Now the sequence of commands can easily be changed by modifying the M-file
task1.m. For example, if you also wishto calculate sin(0.0001)/0.0001, you
can modify the M-file to read
format long
x = [0.1, 0.01, 0.001, 0.0001];
y = sin(x)./x
and then run the modified script by typing task1. Be sure to save your
changes to task1.m first; otherwise, MATLAB will not recognize them. Any
variables that are set by the running of a script M-file will persist exactly
as if you had typed them into the Command Window directly. For example,
the program above will cause all future numerical output to be displayed
with15 digits. To revert to 5-digit format, you would have to type format
short.
Echoing Commands. As mentioned above, the commands in a script M-file
will not automatically be displayed in the Command Window. If you want the
commands to be displayed along withth e results, use echo:
echo on
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
echo off
38 Chapter 3: Interacting with MATLAB
Adding Comments. It is worthwhile to include comments in a lengthly script
M-file. These comments might explain what is being done in the calculation,
or they might interpret the results of the calculation. Any line in a script M-file
that begins with a percent sign is treated as a comment and is not executed by
MATLAB. Here is our new version of task1.m witha few comments added:
echo on
% Turn on 15 digit display
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is 1.
echo off
When adding comments to a script M-file, remember to put a percent sign at
the beginning of each line. This is particularly important if your editor starts
a new line automatically while you are typing a comment. If you use echo
on in a script M-file, then MATLAB will also echo the comments, so they will
appear in the Command Window.
Structuring Script M-Files. For the results of a script M-file to be reproducible,
the script should be self-contained, unaffected by other variables that you
might have defined elsewhere in the MATLAB session, and uncorrupted by
leftover graphics. With this in mind, you can type the line clear all at the
beginning of the script, to ensure that previous definitions of variables do
not affect the results. You can also include the close all command at the
beginning of a script M-file that creates graphics, to close all graphics windows
and start witha clean slate.
Here is our example of a complete, careful, commented solution to the
problem described above:
% Remove old variable definitions
clear all
% Remove old graphics windows
close all
% Display the command lines in the command window
echo on
% Turn on 15 digit display