| Contents | Index |
| On this page… |
|---|
Insert Text and Overwrite Existing Text Change the Case of Selected Text Code Folding — Expand and Collapse Code Constructs |
See also:
On Windows and UNIX platforms, you can enter text in the Editor that inserts within existing text, or that overwrites existing text, as follows:
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.
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.
To change the case of text in the Editor:
Select the text.
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, ...)
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.
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:
Select File > Preferences > Editor/Debugger > Display.
Under General display options, select Highlight current line.
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.

Indenting code makes reading statements such as while loops easier. To set and apply indenting preferences to code in the Editor:
Select File > Preferences > Editor/Debugger > Language.
From the Language drop-down menu, select a language.
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.
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:
Selecting Text > Decrease Indent or Text > Increase Indent, respectively.
Pressing the Tab key or the Shift+Tab key, respectively.
This works differently if you select the Editor/Debugger Tab preference for Emacs-style Tab key smart indenting—when you position the cursor in any line or select a group of lines and press Tab, the lines indent according to smart indenting practices.
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:
Cells used for running sections of code and publishing code
Class code
For and parfor blocks
Function and class help
Function code
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.
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.

If you print a file with one or more collapsed constructs, those constructs are expanded in the printed version of the file.
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:
If a line containing only comments appears at the end of such a function, then the Editor does not include that line when folding the function. MATLAB does not include trailing white space and comments in a function definition that has no explicit end statement.
Code Folding Enabled for Function Code Only illustrates this behavior. Line 13 is excluded from the fold for the foo function.
If a fold for a cell overlaps the function code, then the Editor does not show the fold for the overlapping cell.
The three figures that follow illustrate this behavior. The first two figures, Code Folding Enabled for Function Code Only and Code Folding Enabled for Cells Only illustrate how the code folding appears when you enable it for function code only and then cell code only, respectively. The last figure, Code Folding Enabled for Both Functions and Cells, illustrates the effects when code folding is enabled for both. Because the fold for cell 3 (lines 11–13) overlaps the fold for function foo (lines 4–12), the Editor does not display the fold for cell 3.
Code Folding Enabled for Function Code Only

Code Folding Enabled for Cells Only

Code Folding Enabled for Both Functions and Cells

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.


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:
You can comment the current line or a selection of lines in MATLAB code.
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.
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.
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.
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.

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

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 %.

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 ].](eddebug_comment_multiline.gif)
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 ].](eddebug_comment_partial_result.gif)
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.
Place the cursor anywhere within a block of contiguous lines of comments.
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.
When you wrap comments, the Editor does not wrap the following:
A bulleted list item onto the line that precedes it
A bulleted list item begins with * or # .
Code cell titles (%% followed by a space)
Long strings, such as URLs
This behavior preserves the formatting required for Publishing MATLAB Code Files.
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:
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 |
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.
![]() | Adjust Editor Appearance | File Navigation | ![]() |

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 |