| MATLAB® | ![]() |
| On this page… |
|---|
Using Notebook to Create an M-book Entering MATLAB® Commands in an M-Book Protecting the Integrity of Your Workspace in M-Books |
Using Notebook, you can create a document, called an M-book, that contains text, MATLAB® commands, and the output from MATLAB commands.
You can think of an M-book as a record of an interactive MATLAB session annotated with text, or as a document embedded with live MATLAB commands and output.
This section includes information on performing the following tasks:
To create a new M-book from within MATLAB desktop, type the following in the Command Window:
notebook
If you are running Notebook for the first time, you might need to configure it. See Configuring Notebook for more information.
Notebook starts Microsoft® Word on your system and creates a new M-book, called Document1.
When Word is opening, if a dialog box appears asking you to enable or disable macros, choose to enable macros. Notebook defines Microsoft Word macros that enable MATLAB to interpret the different types of cells that hold MATLAB commands and their output. For more information on macro security, see Configuring Notebook.
Depending on the version of Word you are using, one of the following occurs:
In Word 2002, and 2003, Notebook adds the Notebook menu to the Word menu bar, as shown in the following illustration. Use this menu to access Notebook features.

In Word 2007, Notebook adds the Notebook menu to the Word Add-Ins tab, as shown in the following illustration. Use this menu to access Notebook features.

Microsoft product screen shot reprinted with permission from Microsoft Corporation.
With Notebook running, you can create a new M-book as follows:
In Word 2002, and 2003, select File > New M-book
In Word 2007, select Add-Ins > New M-book, as shown in the following figure.

Microsoft product screen shot reprinted with permission from Microsoft Corporation.
You can use the notebook command to open an existing M-book, as shown in the following code, where filename is the M-book you want to open.
notebook filename
Alternatively, you can double-click an M-book file in a Windows® file management tool, such as Explorer.
When you double-click an M-book, Microsoft Word opens the M-book and starts MATLAB if it is not already running. Notebook adds the Notebook menu to the Word menu bar and adds New M-book to the File menu, as shown in the figure that follows.

To convert a Word document to an M-book, follow the steps provided in one of the following sections, depending on which version of Word you are using:
Microsoft® Word 2002, or 2003.
Create a new M-book.
From the Insert tab, in the Text group, click the arrow next to Object and then click Text from File, as shown in the image that follows.
The Insert File dialog box opens.
In the Insert File dialog box, select the file that you want to convert, and then click OK.

Microsoft product screen shot reprinted with permission from Microsoft Corporation.
Note A good way to learn how to use Notebook is to open the sample M-book, Readme.doc, and try out the various techniques described in this section. You can find this file in the matlabroot/notebook/pc directory. |
You enter MATLAB commands in an M-book the same way you enter text in any other Word document. For example, you can enter the following text in a Word document. The example uses text in Courier Font but you can use any font:
Here is a sample M-book. a = magic(3)
To execute the MATLAB magic command in this document, you must follow the steps described in these sections:
MATLAB displays the output of the command in the Word document in an output cell.
When you work on more than one M-book in a single word processing session, note that:
Each M-book uses the same "copy" of MATLAB.
All M-books share the same workspace.
If you use the same variable names in more than one M-book, data used in one M-book can be affected by another M-book. You can protect the integrity of your workspace by specifying the clear command as the first autoinit cell in the M-book.
An M-book can be thought of as a sequential record of a MATLAB session. When executed in order, from the first MATLAB command to the last, the M-book accurately reflects the relationships among these commands.
If, however, you change an input cell or output cell as you refine your M-book, Notebook does not automatically recalculate input cells that depend on either the contents or the results of the changed cells. As a result, the M-book may contain inconsistent data.
When working on an M-book, you might find it useful to select Evaluate M-book periodically to ensure that your M-book data is consistent. You can also use calc zones to isolate related commands in a section of the M-book, and then use Evaluate Calc Zone to execute only those input cells contained in the calc zone.
Do not use debugging functions or the Editor while evaluating cells with Notebook. Instead, debug M-files from within MATLAB, and then after completing debugging, clear all the breakpoints and access the M-file using Notebook. If you debug while evaluating from Notebook, you might experience problems with MATLAB.
![]() | Using Notebook to Publish to Microsoft® Word | Defining MATLAB® Commands as Input Cells for Notebook | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |