Skip to Main Content Skip to Search
Product Documentation

Evaluating MATLAB Commands in a MATLAB Notebook

Evaluating Input Commands

After you define a MATLAB command as an input cell, or as an autoinit cell, you can evaluate it in your MATLAB Notebook. Use the following steps to define and evaluate a MATLAB command:

  1. Type the command into the notebook as text. For example:

    This is a sample MATLAB Notebook
    
    a = magic(3)
    
  2. Position the cursor anywhere in the command. If the command is embedded in a line of text, use the mouse to select it. Then select Notebook > Define Input Cell or press Alt+D.

    The MATLAB command becomes an input cell. For example:

    This is a sample MATLAB Notebook
    
    [a = magic(3)]
    
  3. Specify the input cell you want to evaluate by selecting it with the mouse or by placing the cursor in it. Then select Notebook > Evaluate Cell or press Ctrl+Enter.

    The input cell is evaluated and the results display in an output cell immediately following the input cell. If there is already an output cell, its contents refresh or update, wherever the output cell is in the notebook. For example:

    This is a sample MATLAB Notebook.
    
    [a = magic(3) ]
    
    [a =
         8     1     6
         3     5     7
         4     9     2  ]
    

The text in the output cell is blue and is enclosed within cell markers. Cell markers are bold, gray brackets. They differ from the brackets that enclose matrices in their size and weight. Error messages appear in red. For information about changing these default formats, see Modifying Styles in the MATLAB Notebook Template.

Evaluating Cell Groups

You evaluate a cell group the same way you evaluate an input cell (because a cell group is an input cell), as follows:

  1. Position the cursor anywhere in the cell or in its output cell.

  2. Select Notebook > Evaluate Cell or press Ctrl+Enter.

For information about creating a cell group, see Defining Cell Groups for a MATLAB Notebook.

When MATLAB evaluates a cell group, the output for all commands in the group appears in a single output cell. By default, the output cell appears immediately after the cell group the first time the cell group is evaluated. If you evaluate a cell group that has an existing output cell, the results appear in that output cell, wherever it is located in the MATLAB Notebook.

The following illustration shows a cell group and the figure created when you evaluate the cell group.

Image of sample cell group in a MATLAB Notebook.

Evaluating a Range of Input Cells

To evaluate more than one MATLAB command contained in different but contiguous input cells, follow these steps:

  1. Select the range of cells that includes the input cells you want to evaluate. You can include text that surrounds input cells in your selection.

  2. Select Notebook > Evaluate Cell or press Ctrl+Enter.

Each input cell in the selection evaluates, new output cells appear or existing ones are replaced.

Evaluating a Calc Zone

To evaluate a calc zone, follow these steps:

  1. Position the cursor anywhere in the calc zone.

  2. Select Notebook > Evaluate Calc Zone or press Alt+Enter.

For information about creating a calc zone, see Defining Calc Zones for a MATLAB Notebook.

By default, the output cell appears immediately after the calc zone the first time the calc zone is evaluated. If you evaluate a calc zone with an existing output cell, the results appear in the output cell wherever it is located in the MATLAB Notebook.

Evaluating an Entire MATLAB Notebook

To evaluate an entire MATLAB Notebook, either select Notebook > Evaluate MATLAB Notebook or press Alt+R.

Evaluation begins at the top of the notebook, regardless of the cursor position and includes each input cell in the file. As it evaluates the file, notebook inserts new output cells or replaces existing output cells.

Controlling Execution of Multiple Commands

When you evaluate an entire MATLAB Notebook, and an error occurs, evaluation continues. If you want to stop evaluation if an error occurs:

  1. Select Notebook > Notebook Options.

    The Notebook Options dialog box opens.

  2. Select the Stop evaluating on error check box, and then click OK.

Using a Loop to Evaluate Input Cells Repeatedly

To evaluate a sequence of MATLAB commands repeatedly, follow these steps:

  1. Use the mouse to select the input cells, including any text or output cells located between them.

  2. Select Notebook > Evaluate Loop or press Alt+L. The Evaluate Loop dialog box displays.

    Image of Evaluate Loop dialog box.

  3. Enter the number of times you want to evaluate the selected commands in the Stop After field, then click Start. The button changes to Stop. Command evaluation begins, and the number of completed iterations appears in the Loop Count field.

You can increase or decrease the delay at the end of each iteration by clicking Slower or Faster. Slower increases the delay. Faster decreases the delay.

To suspend evaluation of the commands, click Pause. The button changes to Resume. Click Resume to continue evaluation.

To stop processing the commands, click Stop. To close the Evaluate Loop dialog box, click Close.

Converting Output Cells to Text

You can convert an output cell to text by undefining cells. If the output is numeric or textual, the cell markers disappear and the cell contents convert to text according to the Microsoft Word Normal style. If the output is graphical, the cell markers disappear and the graphic dissociates from its input cell, but contents of the graphic do not change.

To undefine an output cell, follow these steps:

  1. Select the output cell you want to undefine.

  2. Select Notebook > Undefine Cells or press Alt+U.

Deleting Output Cells

To delete output cells, follow these steps:

  1. Select an output cell, using the mouse, or place the cursor in the output cell.

  2. Select Notebook > Purge Selected Output Cells or press Alt+P.

If you select a range of cells, all the output cells in the selected range disappear, but any associated input cells remain intact.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS