Skip to Main Content Skip to Search
Product Documentation

Editing Code

See also:

Insert Text and Overwrite Existing Text

On Windows and UNIX platforms, you can enter text in the Editor that inserts within existing text, or that overwrites existing text, as follows:

  1. In the Editor, place the cursor where you want to enter text.

    The Editor indicates the current mode as follows:

    • In insert mode, the cursor is a vertical bar and the text OVR appears dimmed in the status bar.

    • In overwrite mode, the cursor is a wide block and the text OVR is not dimmed in the status bar.

  2. Press the Insert key to toggle the typing mode from insert to overwrite mode, or the reverse.

    The Insert key is the default keyboard shortcut for changing the typing mode. For details, on changing keyboard shortcuts, see Customize Keyboard Shortcuts.

Change the Case of Selected Text

To change the case of text in the Editor:

  1. Select the text.

  2. From the Text menu, select one of the following:

    • Change to Upper Case to change all text to capital letters (A ,B, C,...)

    • Change to Lower Case to change all text to small letters (a, b, c, ...)

Undo and Redo Editor Actions

You can undo many of the Editor actions listed in Edit and Text menus. Select Edit > Undo. You can undo multiple times in succession until there are no remaining actions to undo. Select Edit > Redo to reverse an undo operation.

Highlight Current Line

You can set a preference to highlight the current line, that is, the line with the cursor. This helps you see where copied text will be inserted when you paste, for example.

To highlight the current line:

  1. Select File > Preferences > Editor/Debugger > Display.

  2. Under General display options, select Highlight current line.

  3. If you want a highlight color other than green, click the color palette and choose a different color.

In the following image, the current line is highlighted in the default highlighting color—green.

Image of Editor showing that the line where cursor is located is highlighted in green.

Indent Code

Indenting code makes reading statements such as while loops easier. To set and apply indenting preferences to code in the Editor:

  1. Select File > Preferences > Editor/Debugger > Language.

  2. From the Language drop-down menu, select a language.

  3. Select or clear Apply smart indenting while typing, depending on whether you want indenting applied automatically, as you type.

    If you clear this option, you can manually apply indenting by selecting the lines in the Editor to indent, right-clicking, and then selecting Smart Indent from the context menu.

  4. Do one of the following:

    • If you chose any language other than MATLAB in step 2, click OK.

    • If you chose MATLAB in step 2, select a Function indenting format, and then click OK. Function indent formats are:

      • Classic — The Editor aligns the function code with the function declaration.

      • Indent nested functions — The Editor indents the function code within a nested function.

      • Indent all functions — The Editor indents the function code for both main and nested functions.

    The following image illustrates the function indenting formats.

Regardless of whether you apply indenting automatically or manually, you can move selected lines further to the left or right, by doing one of the following:

Code Folding — Expand and Collapse Code Constructs

Code folding is the ability to expand and collapse certain MATLAB programming constructs. This improves readability when a file contains numerous subfunctions or other blocks of code that you want to hide when you are not currently working with that part of the file. MATLAB programming constructs include:

To see the entire list of constructs, select File > Preferences > Editor/Debugger > Code Folding.

To expand or collapse code, click the plus or minus sign that appears to the left of the construct.

To expand or collapse all of the code in a file, place your cursor anywhere within the file, right-click, and then select Code Folding > Expand All or Code Folding > Collapse All from the context menu.

View Folded Code in a Tooltip

You can view code that is currently folded by positioning the pointer over its ellipsis . The code appears in a tooltip.

The following image shows the tooltip that appears when you place the pointer over the ellipsis on line 6 of collatzall.m when the collatzplot_new function is folded.

Image of Editor with Tooltip displaying code within the folded function.

Print Files with Collapsed Code

If you print a file with one or more collapsed constructs, those constructs are expanded in the printed version of the file.

Code Folding Behavior for Functions that Have No Explicit End Statement

If you enable code folding for functions and a function in your code does not end with an explicit end statement, you see the following behavior:

Code Folding Enabled for Function Code Only

Code Folding Enabled for Cells Only

Code Folding Enabled for Both Functions and Cells

Effect of Syntax Errors on Code Folding

If your code contains syntax errors, the code folding indicators might appear to be in the wrong location. For example, suppose your code currently appears as shown in the first figure that follows. If you delete the while statement, it introduces a syntax error at line 3, as shown in the second figure that follows. Notice that the minus sign remains in the same location it held for the syntactically correct code. After you correct the syntax error, the Editor adjusts and displays the code folding indicators appropriately.

Image of Editor showing unfolded code within a while loop.

Image of Editor showing that the minus sign that was in place for the while loop, remains on the same line number after the while statement is deleted.

Add Comments

Comments in MATLAB code are strings or statements that do not execute. Add comments in a file to describe the code or how to use it. Comments determine what text displays when you run help for a file name. Use comments when testing your files or looking for errors—temporarily turn lines of code into comments to see how the file runs without those lines. These topics provide details:

Commenting in MATLAB Code Using the MATLAB Editor

You can comment the current line or a selection of lines in MATLAB code.

  1. For a single line, position the cursor in that line. For multiple lines, click in the line and then drag or Shift+click to select multiple lines.

  2. Right-click and select Comment from the context menu.

    The Editor adds a comment symbol, % at the start of each commented line.

To uncomment the current line or a selected group of lines, right-click and select Uncomment from the context menu.

Commenting in Java and C/C++ Files Using the MATLAB Editor

To add comments to selected lines in Java and C/C++ files, select Text > Comment. The Editor adds the // symbols at the front of the selected lines.

To remove comments from selected lines in Java and C/C++ files, select Text > Uncomment. The Editor removes the // symbols from the front of the selected lines.

Commenting in MATLAB Code Using Any Text Editor

You can make any line in MATLAB code a comment by typing % at the beginning of the line. To put a comment within a line, type % followed by the comment text. MATLAB software treats all the information after the % on a line as a comment. For instance, if the following line appears in a file, then MATLAB ignores the line when you run the file:

% This is a comment.

To uncomment any line, delete the comment symbol, %.

To comment a contiguous group of lines, type %{ before the first line and %} after the last line you want to comment. This is referred to as a block comment. The lines that contain %{ and %} can contain spaces, but no other text. Remove the block comment symbols, %{ and %}, to uncomment the lines.

This example shows some lines of code commented out. When you run the file, the commented lines do not execute. This is useful when you want to identify the section of a file that is not working as expected.

Image of block comment.

You can create a nested block comment, as shown in the following example.

Image showing nested block comments.

Commenting Out Part of a Statement

To comment out the end of a statement, put the comment character, %, before the comment. When you run the file, MATLAB software ignores any text on the line after the %.

Image of comment within a line. The statement is a = zeros(10) % Initialize matrix. Text after the % is considered to be a comment.

To comment out text within a multiline statement, you must use an ellipsis (...); the comment character (%) does not work in this context. MATLAB ignores any text appearing after the ellipsis on a line and continues processing on the next line. This effectively makes a comment out of anything on the current line that follows the ellipsis. The following example comments out the Middle Initial line.

Image of multiline statement, where one line is a comment. The first line is header = [\xd5 Last Name, \xd5 ... The second line is \xd5 First Name, \xd5 ... The third line is ... \xd5 Middle Initial, \xd5 . The fourth line is \xd5 Title\xd5 ].

Notice that Middle Initial is green, which is the syntax highlighting color for a comment.

MATLAB effectively runs

Image of lines that MATLAB runs. First line is headers=[\xd5 Last Name, \xd5 ... Second line is \xd5 First Name, \xd5 ... Last line is \xd5 Title\xd5 ].

Wrap Comments

By default, as you type them in the Editor, comments wrap whenever they reach a column width of 75. In addition, you can manually wrap comment lines, such as those that you paste into a file.

Wrap Comments Manually

  1. Place the cursor anywhere within a block of contiguous lines of comments.

  2. From the context menu, select Wrap Comments.

    MATLAB wraps all the comments in the block. To wrap only a subset of the comments, select the subset before you wrap it.

To change the column at which comments wrap, or to disable automatic comment wrapping, set Comment Formatting preferences using Editor/Debugger Language Preferences.

Exclusions from Comment Wrapping

When you wrap comments, the Editor does not wrap the following:

This behavior preserves the formatting required for publishing.

Colors in the MATLAB Editor — What Do They Mean?

Colors in the Editor help you to read code, identify code elements, and evaluate sections of code. To find out why certain portions of your code appear in color, how to change the color, or learn more about the features highlighted in color, see the table that follows.

Sample (Using Default Colors)What the Color Indicates

Different types of language elements, such as keywords, comments, and strings appear in different colors. This is called syntax highlighting.

See also, Highlight Syntax to Help Ensure Correct Entries in the Editor

Teal blue characters indicate variables with shared scope

Sky blue shading indicates function, subfunction, or variable names that match the name in which the cursor is currently placed.

See also, Avoid Variable and Function Scoping Problems.

Orange and red wavy underlines indicate warning and error conditions, respectively.

Orange shading indicates coding issues that MATLAB can correct for you.

See also, Check Code for Errors and Warnings

Yellow highlighting indicates code cells, which:

  • Help you visually identify subsections of code

  • Enable you to publish and run subsections of code.

See also, What Are Code Cells?

Red dots represent breakpoints, which you use in debugging.

If you attempt to run your code, MATLAB stops at the first breakpoint it encounters.

See also, Debugging Process and Features

A gray vertical line indicates the location of a particular column in the Editor that you can use to limit line widths.

The Editor does not enforce the limit.

See also, Right-Side Text Limit Indicator

Code Contains %#ok — What Does That Mean?

If code contains the string %#ok at the end of a line of code, it indicates that one or more Code Analyzer messages is suppressed. For more information, see Understand Code Containing Suppressed Messages.

  


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