Chapter 6
M-Books
MATLAB is exceptionally strong in linear algebra, numerical methods, and
graphical interpretation of data. It is easily programmed and relatively easy
to learn to use. As suchit has proven invaluable to engineers and scientists
who are working on problems that rely on scientific techniques and methods at
which MATLAB excels. Very often the individuals and groups that so employ
MATLAB are primarily interested in the numbers and graphs that emerge
from MATLAB commands, processes, and programs. Therefore, it is enough
for them to work in a MATLAB Command Window, from which they can eas-
ily print or export their desired output. At most, the production technique
described in Chapter 3 involving diary files is sufficient for their presentation
needs.
However, other practitioners of mathematical software find themselves with
two additional requirements. They need a mathematical software package em-
bedded in an interactive environment — one in which the output is not nec-
essarily “linear”, that is, one that they can manipulate and massage without
regard to chronology or geographical location. Second, they need a higher-level
presentation mode, which affords graphics integrated with text, with different
formats for input and output, and one that can communicate effortlessly with
other software applications. Some of MATLAB’s competitors have focused on
such needs in designing the interfaces (or front ends) behind which their math-
ematical software runs. MATLAB has decided to concentrate on the software
rather than the interface — and for the reasons and purposes outlined above,
that is clearly a wise decision. But for academic users (both faculty and espe-
cially students), for authors, and even for applied scientists who want to use
MATLAB to generate slick presentations, the interface demands can become
very important. For them, MATLAB has provided the M-book interface, which
we describe in this chapter.
91
92
Chapter 6: M-Books
The M-book interface allows the user to operate MATLAB from a special
Microsoft Word document instead of from a MATLAB Command Window. In
this mode, the user should think of Word as running in the foreground and
MATLAB as running in the background. Lines that you enter into your Word
document are passed to the MATLAB engine in the background and executed
there, whereupon the output is returned to Word (through the intermediary of
Visual Basic
), and then both input and output are automatically formatted.
One obtains a living document in the sense that one can edit the document as
one normally edits a word processing document. So one can revisit input lines
that need adjustment, change them, and reexecute on the spot — after which
the old outdated output is automatically overwritten with new output. The
graphical output that results from MATLAB graphics commands appear in
the Word document, immediately after the commands that generated them.
Erroneous input and output are easily expunged, enhanced formatting can
be done in a way that is no more complicated than what one does in a word
processor, and in the end the result of your MATLAB session can be an at-
tractive, easily readable, and highly informative document. Of course, one can
“cheat” by editing one’s output — we shall discuss that and other pitfalls and
strengths in what follows.
Enabling M-Books
To run the M-book interface you must have Microsoft Word on your com-
puter. It is possible to run the interface with earlier versions of Word, but
we find that it works best if you have Word 97. (In fact, we find that it
runs better in Word 97 than it does in Word 2000, though the difference is
not usually significant.) The interface is enabled when you install MATLAB.
This is done in one of three ways depending on which version of MATLAB
you have. In some instances, during installation, you will be prompted to
enter the location of the Word executable file and the Word template direc-
tory. These are usually easily located; for example, on many PCs the former
is in MSOffice\Office\Winword.exe, and the latter is in MSOffice\
Templates. You may also be asked to specify a template file — in that case,
select normal.dot in the Templates directory. The installation program will
create a new template called m-book.dot, which is the Word template file as-
sociated withM-book documents.
✓
If you don’t know where the Word files are located on your PC, go to Find
from the Start menu on the Task Bar, and search your hard drive for the
files Winword.exe and normal.dot.
Starting M-Books
93
In other instances, you may not notice any prompt for Word information
during installation. This can mean that your computer found the Word
executable and template information and set up the associations automat-
ically; or it can mean that it ignored the M-book configuration completely.
In either eventuality, it is best, after installation, to type notebook -setup
from the Command Window. Follow the ensuing instructions, which will be
essentially the same as in the first possibility described in the last paragraph.
Starting M-Books
The most common way to start up the M-book interface is to type notebook
at the Command Window prompt. This is the only way to start the M-book
interface if it is your first foray into the venue. After you type notebook,
you will see Microsoft Word launchand a blank Word document will fill your
screen. We will refer to this document as an M-book. The difference between a
blank M-book and a normal Word document is only apparent if you peruse the
menu bar. There you will see an entry that is not present in a normal Word
document — namely, the Notebook menu. Click on it and examine the menu
items that appear. We will describe each of them and their functions in our
discussion below. If this is not your first experience with M-books, and you
have already saved an M-book, say under the name Problem1.doc, then you
can open it by typing notebook Problem1.doc at the Command Window
prompt. Even though you may not see it, the MATLAB Command Window is
alive, but it is hidden behind the M-book.
➱
On some systems, you may see a DOS command window appear after
typing notebook, but before the M-book appears. We recommend
that you close that window before working in the M-book.
✓
For M-books to work properly, you need to have “Macros Enabled” in your
Word installation. If an M-book opens as a regular Word document, without
M-book functionality, it probably means that macros have been disabled. To
enable them, first close the document (without saving changes), then go to
Tools : Macro : Security... from the Word menu bar, and reset your security
level to Medium or Low. Then reopen the M-book.
✓
An alternate, and on some systems (especially networked systems) a
preferable, launchmethod is first to open a previously saved M-book —
either directly through File : Open... in Word or by double-clicking on the
file name in Windows Explorer. Word recognizes that the document is an
M-book, so automatically launches MATLAB if it is not already running. A
94
Chapter 6: M-Books
word of caution: If you have more than one version of MATLAB installed,
Word will launch the version you installed last. To override this, you can
open the MATLAB version you want before you open the M-book.
You can now type into the M-book in the usual way. In fact you could pre-
pare a document in this screen in precisely the same manner that you would
in a normal Word screen. The background features of MATLAB are only ac-
tivated if you do one of two things: either access the items in the Notebook
menu or press the key combination
CTRL+ENTER
. Type into your M-book the
line 23/45 and press
CTRL+ENTER
. After a short delay you will see what you
entered change font to bold New Courier, encased in brackets, and then the
output
ans =
0.5111
will appear below, also in New Courier font (but not bold). It is also likely that
the input and output will be colored (the input in green, the output in blue).
Your cursor should be on the line following the output, but if it is at the end
of the output line, move it down a line and type solve(’xˆ2 - 5*x+5=
0’) followed by
CTRL+ENTER
. After some thought MATLAB feeds the answer
to the M-book:
ans=
[5/2+1/2*5^(1/2)]
[5/2-1/2*5^(1/2)]
Finally, try typing ezplot(’xˆ3 - x’), then
CTRL+ENTER
, and watchthe
graph appear. At this point your M-book should look like Figure 6-1.
You may note that your commands take a little longer to evaluate than
they would inside a normal MATLAB Command Window. This is not sur-
prising considering the amount of information that is passing back and forth
between MATLAB and Word. Continue entering MATLAB commands that are
familiar to you (always followed by
CTRL+ENTER
), and observe that you obtain
the output you expect, except that it is formatted and integrated into your
M-book.
✓
If you want to start a freshM-book, click on File : New M-book in the Menu
Bar, or File : New, and then click on m-book.dot.
Working with M-Books
95
Figure 6-1: A Simple M-Book.
Working with M-Books
You interact withdata in your M-book in two ways — via the keyboard or
through the menu bar.
Editing Input
Place your cursor in the line containing the second command of the previous
section — where we solved the quadratic equation
x
2
− 5x + 5 = 0.
Click to the left of the equal sign, hit
BACKSPACE
, type 6 (that is, replace the
second 5 by a 6), and press
CTRL+ENTER
. You will see your output replaced by
ans =
[2]
[3]
By changing the quadratic equation we have altered its roots. You can edit
any of the input lines in your M-book in this way, including the one that
generated the graph. See what happens if you click in the ezplot command
line, change the cubic expression, and press
CTRL+ENTER
.