| MATLAB® | ![]() |
| On this page… |
|---|
Class, Function, or Subfunction |
Note You can specify the default behaviors for some of these features—see Preferences. |
Some entries appear in different colors to help you better find matching elements, such as if/else statements. Similarly, unterminated strings have a different color than terminated strings. This is called syntax highlighting and is used in the Command Window and History, as well as in the Editor. For more information, see the Command Window documentation for Syntax Highlighting.
When you paste or drag a selection from the Editor to another application, such as Microsoft® Word, the pasted text maintains the syntax highlighting colors and font characteristics from the Editor. MATLAB® software pastes the selection to the clipboard in RTF format, which many Microsoft Windows® and Macintosh® applications support.
You can set an indenting preference so that program control entries are automatically indented to make reading loops, such as while/end statements, easier. To do so, select File > Preferences > Editor/Debugger > Language, and select a Language, for example, M. For Indenting for Enter key, select Smart indenting or Block indent, and then click OK. Use No indent instead if you want to indent manually. For more information about indenting preferences, click Help in the Preferences dialog box. Specify the indenting size and other options by selecting File > Preferences > Editor/Debugger > Tab.
You can manually apply smart indenting to selected lines—select the lines and then select Smart Indent from the Text menu, or right-click and select it from the context menu. This feature indents lines that start with keyword functions or that follow lines containing certain keyword functions. Smart indenting can help you to follow the code sequence.
To move the current or selected lines further to the left, select Decrease Indent from the Text menu. To move the current or selected lines further to the right, select Increase Indent from the Text menu.
You can also indent a line by pressing the Tab key at the start of a line. Or select a line or group or lines and press the Tab key. Press Shift+Tab to decrease the indent for the selected lines. 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.
For more information about manual indenting, select File > Preferences > Editor/Debugger > Tab and then click Help.
If you select the language preference for smart indent, you can select from three indenting options when you enter a subfunction or a nested function (a function within a function) in the Editor. For details, see Function Indenting Format.
Line numbers display along the left side of the Editor window. You can elect not to show the line numbers using preferences. For details, select File > Preferences > Editor/Debugger > Display, and then click Help.
The line and column numbers for the current cursor position are shown in the far right side of the status bar in the Editor.
You can set a preference to highlight the current line, that is the line with the caret (also called the cursor). This is useful, for example, to help you see where copied text will be inserted when you paste.
To highlight the current line, select File > Preferences > Editor/Debugger > Display, and under General display options, select the check box for Highlight current line. You can also specify the color used to highlight the line.

By default, a light red vertical line (rule) appears at column 75 in the Editor, providing a cue as to when a line becomes wider than desired, which is useful if you plan to print the file, for example. You can hide the line or change the column number at which it appears. For more information, select File > Preferences > Editor/Debugger > Display, and then click Help.
The right side of the Editor status bar shows the class, function, or subfunction where the cursor is currently placed, depending on the type of file you are viewing, as follows:
Class file — The name of the class followed by the name of the current function (if any) that the cursor is within. This is true regardless of the type of function in which the cursor is placed (nested, in a methods block, outside a classdef file, and so on).
Function file — The name of the main function followed by the name of the current function the cursor is within (if any). This is true regardless of the type of function in which the cursor is placed (subfunction or nested).
Code folding is the ability to expand and collapse certain M-file programming constructs. This improves readability when an M-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.
You can set preferences to enable or disable the ability to expand and collapse the following M-file programming constructs:
Block comments
Cells used for rapid code iteration and publishing
Class code
Class enumeration blocks
Class event blocks
Class method blocks
Class properties blocks
For and parfor blocks
Function and class help
Function code
If/else blocks
Switch/case blocks
Try/catch blocks
While blocks
By default, code folding is enabled for all programming constructs except if/else blocks and switch/case blocks. Select File > Preferences > Editor/Debugger > Code Folding, and then click Help for details on setting preferences.
When you fold a construct, all the code associated with that construct is collapsed such that the
Editor displays only the first line of the construct prepended by the expand
icon (
) and appended with an ellipsis icon
(
) to indicate there is more code. When you expand a construct, all
the code associated with that construct appears and the first line
of the construct is prepended with the collapse icon (
).
The following image shows the collatzall and collatzplot_new functions collapsed and the collatz function code expanded.

When you expand a function or class, but collapse its associated
help code, the Editor displays all the function or class code and just the H1 line of
the help code. The H1 line ends with a commented ellipsis icon
to indicate there is additional help
code, as shown in the following image.

To expand code for a construct that is currently collapsed, do one of the following:
Click the expand icon
to the left of the construct that you
want to expand.
Place your cursor in the code that you want to expand, right-click, and then select Code Folding > Expand from the context menu.
To collapse code for a construct that is currently expanded, do one of the following:
Click the collapse icon
to the left of the construct that you
want to collapse.
Place your cursor in the code that you want to collapse, right-click, and then select Code Folding > Collapse from the context menu.
To expand or collapse all of the code in an M-file, place your cursor anywhere within the M-file, right-click, and then select Code Folding > Expand All or Code Folding > Collapse All from the context menu.
For information on the structure of an M-file, including a description of a function definition line and an H1 line, see Basic Parts of an M-File in the Programming Fundamentals documentation.
You can view code that is currently folded by positioning the
pointer over its ellipsis icon
. The code appears in a ToolTip. This
lets you quickly view the code without unfolding it.
The following image shows the ToolTip that appears when you place the pointer over the ellipsis icon on line 6 of collatzall.m when the collatzplot_new function is folded.

Be aware of the following:
You can change the current code folding settings, by selecting File > Preferences > Editor/Debugger > Code Folding. If needed, click Help for assistance.
By default, the first time you open an M-file that existed before MATLAB Version 7.5 (R2007b) using MATLAB Version 7.5 (R2007b) or later, code folding is enabled and all constructs are expanded.
Constructs that are collapsed when you close an M-file remain collapsed when you reopen the file.
If you copy a collapsed construct from one region of an M-file and paste it in another region, the construct is expanded in the pasted location.
If you print a file with one or more collapsed constructs, those constructs are expanded in the printed version of the file.
If your code contains syntax errors, the code folding icon or its end indicator appear to be placed 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 collapse icon 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 icon appropriately.


You can simultaneously display two different parts of a file in the Editor. This makes it easy to compare different lines in a file or to copy and paste from one part of a file to another.
Split the screen horizontally by selecting Window > Split Screen > Top/Bottom. Or to split it vertically, select Left/Right.
Alternatively, when there is a scroll bar, split the document into top and bottom views by dragging the splitter bar, as shown in the following illustration, down from above the vertical scroll bar. Similarly, to split into left and right views, drag the splitter bar from the left of the horizontal scroll bar. The pointer assumes a double-headed arrow shape when you position it on the splitter bar.



Resize of the views by dragging the splitter. The pointer assumes an arrow shape when you position it on the splitter.
Only one view is active at any time, meaning, you will see only the cursor in one of the views. To change the active view, select Window > Split Screen > Switch Focus or its keyboard equivalent, which is shown with the menu item. The cursor returns to its last position in that view.
Make changes to the document in either view. Both views of the file are always current, so you see the changes in either view.
You split each open document individually, so there can be multiple views at once. You can split some documents horizontally, others vertically, and leave others unsplit. When you open a document, it always opens unsplit, regardless of its split status when you last had it open.
You can remove a document split using any of these methods:
Drag the splitter to an edge of the window.
Double-click the splitter.
Select Window > Split > Screen > Off.
See also Summary of Actions for Arranging Documents for instructions to display multiple documents simultaneously.
![]() | Entering Statements in the Editor | Navigating in an M-File | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |