Using MATLAB (779505), страница 36
Текст из файла (страница 36)
. . . . . . .Evaluating a Range of Input Cells . . . . . . .Evaluating a Calc Zone . . . . . . . . . . .Evaluating an Entire M-Book . . . . . . . .Using a Loop to Evaluate Input Cells RepeatedlyConverting Output Cells to Text . . . . . . .Deleting Output Cells. . . . . .
. . . . .................................10-1210-1310-1410-1410-1510-1510-1610-17Printing and Formatting an M-Book .Printing an M-Book . . . . . . . . .Modifying Styles in the M-Book TemplateChoosing Loose or Compact Format . . .Controlling Numeric Output Format . . .Controlling Graphic Output . . . . . .........................10-1810-1810-1810-1910-2010-20Configuring Notebook................... . . . .
. . . . . . . . . 10-24Notebook Command Reference. . . . . . . . . . 10-2610Using NotebookNotebook allows you to access MATLAB’s numeric computation andvisualization software from within a word processing environment (MicrosoftWord). Using Notebook, you can create a document, called an M-book, thatcontains text, MATLAB commands, and the output from MATLAB commands.You can think of an M-book as a record of an interactive MATLAB sessionannotated with text or as a document embedded with live MATLAB commandsand output. Notebook is useful for creating electronic or printed records ofMATLAB sessions, class notes, textbooks or technical reports.This section provides information about:• “Notebook Basics” on page 10-3• “Defining MATLAB Commands as Input Cells” on page 10-8• “Evaluating MATLAB Commands” on page 10-12• “Printing and Formatting an M-Book” on page 10-18• “Configuring Notebook” on page 10-24• “Notebook Command Reference” on page 10-26.10-2Notebook BasicsNotebook BasicsThis section introduces basic Notebook capabilities, including:• “Creating an M-Book” on page 10-3• “Entering MATLAB Commands in an M-Book” on page 10-6• “Protecting the Integrity of Your Workspace” on page 10-6• “Ensuring Data Consistency” on page 10-7Creating an M-BookThis section describes how to:• Create an M-book from MATLAB• Create an M-book while running Notebook• Open an existing M-book• Convert a Word document into an M-bookCreating an M-Book from MATLABTo create a new M-book from within MATLAB, typenotebookat the prompt.
If you are running Notebook for the first time, you may need toconfigure it. See “Configuring Notebook” on page 10-24 for more information.Notebook starts Microsoft Word on your system and creates a new M-book,called Document1. Notebook adds the Notebook menu to the Word menu bar.You use this menu, illustrated below, to access Notebook commands.Note Notebook defines Microsoft Word macros that enable MATLAB tointerpret the different types of cells that hold MATLAB commands and theiroutput.10-310Using NotebookCreating an M-Book While Running NotebookWith Notebook running, you can also create a new M-book by selecting NewM-book from the Word File menu.10-4Notebook BasicsOpening an Existing M-BookYou can also use the notebook command to open an existing M-booknotebook filenamewhere filename is the M-book you want to open, or you can simply double-clickon an M-book file.When you double-click on an M-book, Microsoft Word opens the M-book andstarts MATLAB, if it is not already running.
Notebook adds the Notebookmenu to the Word menu bar.Converting a Word Document to an M-BookTo convert a Word document to an M-book, follow these steps:1 Create a new M-book.2 From the Insert menu, select the File command.10-510Using Notebook3 Select the file you want to convert.4 Click on the OK button.Entering MATLAB Commands in an M-BookNote 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.
Youcan find this file in the $MATLAB\notebook\pc directory, where $MATLABrepresents your installation directory.You enter MATLAB commands in an M-book the same way you enter text inany other Word document. For example, you can enter the following text in aWord document. The example uses text in Courier Font but you can use anyfont.Here is a sample M-book.a = magic(3)To execute the MATLAB magic command in this document, you must:• Define the command as an input cell• Evaluate the input cell.MATLAB displays the output of the command in the Word document in anoutput cell.Protecting the Integrity of Your WorkspaceWhen 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 oneM-book can be affected by another M-book.
You can protect the integrity of your10-6Notebook Basicsworkspace by specifying the clear command as the first autoinit cell in theM-book.Ensuring Data ConsistencyAn 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, theM-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 eitherthe contents or the results of the changed cells.
As a result, the M-book maycontain inconsistent data.When working on an M-book, you might find it useful to select the EvaluateM-book command periodically to ensure that your M-book data is consistent.You could also use calc zones to isolate related commands in a section of theM-book. You can then use the Evaluate Calc Zone command to execute onlythose input cells contained in the calc zone.10-710Using NotebookDefining MATLAB Commands as Input CellsTo define a MATLAB command in a Word document as an input cell:1 Type the command into the M-book as text. For example,This is a sample M-book.a = magic(3)2 Position the cursor anywhere in the command and choose the Define InputCell command from the Notebook menu or press Alt+D.
If the command isembedded in a line of text, use the mouse to select it. Notebook defines theMATLAB command as an input cell.This is a sample M-book.[a = magic(3)]Note how Notebook changes the character font of the text in the input cell to abold, dark green color and encloses it within cell markers. Cell markers arebold, gray brackets. They differ from the brackets used to enclose matrices bytheir size and weight.
For information about changing these default formats,see “Modifying Styles in the M-Book Template” on page 10-18.For information about defining other types of input cells, see:• “Defining Cell Groups” on page 10-8• “Defining Autoinit Input Cells” on page 10-10• “Defining Calc Zones” on page 10-10• “Converting an Input Cell to Text” on page 10-11For information about evaluating the input cells you define, see “EvaluatingMATLAB Commands” on page 10-12.Defining Cell GroupsYou can collect several input cells into a single input cell. This is called a cellgroup.
Because all the output from a cell group appears in a single output cellthat Notebook places immediately after the group, cell groups are useful whenseveral MATLAB commands are needed to fully define a graphic.10-8Defining MATLAB Commands as Input CellsFor example, if you define all the MATLAB commands that produce a graphicas a cell group and then evaluate the cell, Notebook generates a single graphicthat includes all the graphic components defined in the commands. If insteadyou define all the MATLAB commands that generate the graphic as separateinput cells, evaluating the cells generates multiple graphic output cells.See “Evaluating Cell Groups” on page 10-13 for information about evaluatinga cell group.
For information about undefining a cell group, see “Ungroup CellsCommand” on page 10-32.Creating a Cell GroupTo create a cell group:1 Use the mouse to select the input cells that are to make up the group.2 Select the Group Cells command from the Notebook menu or press Alt+G.Notebook converts the selected cells into a cell group and replaces cell markerswith a single pair that surrounds the group.This is a sample cell group.[datea = magic(3) ]Note the following:• A cell group cannot contain text or output cells.
If the selection includesoutput cells, Notebook deletes them.• If the selection includes text, Notebook places the text after the cell group.However, if the text precedes the first input cell in the selection, Notebookleaves it where it is.• If you select part or all of an output cell but not its input cell, Notebookincludes the input cell in the cell group.When you create a cell group, Notebook defines it as an input cell unless itsfirst line is an autoinit cell, in which case Notebook defines the group as anautoinit cell.10-910Using NotebookDefining Autoinit Input CellsYou can use autoinit cells to specify MATLAB commands to be automaticallyevaluated each time an M-book is opened. This is a quick and easy way toinitialize the workspace. Autoinit cells are simply input cells with the followingadditional characteristics:• Notebook evaluates the autoinit cells when it opens the M-book.• Notebook displays the commands in autoinit cells using dark bluecharacters.Autoinit cells are otherwise identical to input cells.Creating an Autoinit CellYou can create an autoinit cell in two ways:• Enter the MATLAB command as text, then convert the command to anautoinit cell by selecting the Define AutoInit Cell command from theNotebook menu.• If you already entered the MATLAB command as an input cell, you canconvert the input cell to an autoinit cell.
Either select the input cell orposition the cursor in the cell, then select the Define AutoInit Cell commandfrom the Notebook menu.See “Evaluating MATLAB Commands” on page 10-12 for information aboutevaluating autoinit cells.Defining Calc ZonesYou can partition an M-book into self-contained sections, called calc zones. Acalc zone is a contiguous block of text, input cells, and output cells. Notebookinserts Microsoft Word section breaks before and after the section to define thecalc zone. The section break indicators include bold, gray brackets todistinguish them from standard Word section breaks.You can use calc zones to prepare problem sets, making each problem aseparate calc zone that can be created and tested on its own.















