Desktop Tools and Development Environment Previous page   Next Page

Defining Cells

Cell features operate on contiguous lines of code you want to evaluate as a whole in an M-file script, called cells. To define a cell, first be sure that cell mode is enabled (see step 1). Position the cursor just before the line you want to start the cell and then select Cell -> Insert Cell Divider or click the Insert Cell Divider button . After the cursor, MATLAB inserts a line containing two percent signs (%%), which is the "start new cell" indicator to MATLAB. A cell consists of the line starting with %% and the lines that follow, up to the start of the next cell, which is identified by %% at the start of a line.

You can also define a cell by entering two percent signs (%%) at the start of the line where you want to begin the new cell. Alternatively, select the lines of code to be in the cell and then select Cell -> Insert Cell Dividers Around Selection.

You can define a cell at the start of a new empty file, enter code for the cell, define the start of the next cell, enter its code, and so on. Redefine cells by defining new cells, removing existing cells, and moving lines of code.

MATLAB will not execute the code in lines beginning with %%, so be sure to put any executable code for the cell on the following line. For program control statements, such as if ... end, a cell must contain both the opening and closing statements, that is, it must contain both the if and the end statements.

Note that the first cell in a file does not have to begin with %%. MATLAB automatically understands any lines above the first %% line to be a cell. If there are no cell dividers in an M-file, MATLAB understands the entire file to be a single cell.

Cell Titles and Highlighting

After the %%, type a space followed by a description of the cell. The Editor/Debugger emphasizes the special meaning of the start of a cell by making any text following the percent signs and space bold. The text on the %% line is called the cell title (like a section title). Including cell titles is optional, however, they improve readability of the file and are used for cell publishing features.

When the cursor is positioned in any line within a cell, the Editor/Debugger highlights the entire cell with a yellow background. This identifies it as the current cell. For example, it is used when you select the Evaluate Current Cell option on the Cell menu.

If you want cell titles to appear in plain rather than bold text, or if you do not want yellow highlighting for the current cell, change these preferences. Select File -> Preferences -> Editor/Debugger -> Display and change the appropriate Cell display options.

Example--Define Cells

This examples defines two cells for a simple M-file called sine_wave, shown in the following code and figure. The first cell creates the basic results, while the second label the plot. The two cells in this example allow you to experiment with the plot of the data first, and then when that is final, change the plot properties to affect the style of presentation.

% Define the range for x.
% Calculate and plot y = sin(x).
x = 0:1:6*pi;
y = sin(x);
title('Sine Wave','FontWeight','bold')
set(gcf, 'MenuBar', 'none')

Image of file sine_wave.m in Editor before defining cells.

  1. Select Cell -> Enable Cell Mode, if it is not already enabled.
  2. Position the cursor at the start of the first line. Select Cell -> Insert Cell Divider.
  1. The Editor/Debugger inserts %% as the first line and moves the rest of the file down one line. All lines are highlighted in yellow, indicating that the entire file is a single cell.

  1. Enter a cell title following the %%. Type a space first, followed by the description.
  2. Position the cursor at the start of line 7, title.... Select Cell -> Insert Cell Divider.
  1. The Editor/Debugger inserts a line containing only %% at line 7 and moves the remaining lines down by one line. Lines 7 through 12 are highlighted in yellow, indicating they comprise the current cell.

  1. Enter a cell title for the new cell. On line 7, type a space after the %%, followed by the description
  1. Save the file. The file appears as shown in this figure.

    Image of sine_wave.m file in Editor after defining cells. Each cell begins with %% followed by an associated title.

Removing Cells

To remove a cell, delete one of the percent signs (%) from the line that starts the cell. This changes the line from a cell to a standard comment and merges the cell with the preceding cell. You can also just delete the entire line that contains the %%.

Previous page  Rapid Code Iteration Using Cells Navigating and Evaluating with Cells Next page

© 1994-2005 The MathWorks, Inc.