| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Class, Function, or Subfunction |
Note You can specify the default behaviors for some of these features—see Specifying Options for MATLAB Using 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, Command History, and the Editor. For more information, see the Command Window documentation for Highlighting Syntax to Help Ensure Correct Entries.
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.
This section describes how to apply indenting to code in the Editor.
Set an indenting preference so that program control entries indent automatically to make reading statements such as while loops easier:
Select File > Preferences > Editor/Debugger > Language.
From the Language drop-down menu, select a language.
Under Indenting, select Apply smart indenting while typing.
Click OK.
If you want to indent manually, clear Apply smart indenting while typing in step 3. 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.
Indenting can help you to read the code sequence. Manually apply smart indenting, so that lines indent if they start with a keyword function or if they follow lines containing certain keyword functions:
Select the lines to indent.
Select Text > Smart Indent or right-click and select Smart Indent from the context menu.
Adjust the text by doing one of the following:
Move the current or selected lines further to the left, by selecting Text > Decrease Indent.
Move the current or selected lines further to the right, by selecting Text > Increase Indent.
Additional methods for indenting a line are as follows:
Press the Tab key at the start of a line.
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.
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 display 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 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.

By default, a light gray vertical line (rule) appears at column 75 in the Editor, indicating where a line becomes wider than desired. This is useful, for example, if you want to keep each line below a limit imposed by another text editor in which you intend to view the code. You can change the width and color of the vertical line, as well as the column number at which the vertical line appears. If you want, you can hide the vertical line. For more information, select File > Preferences > Editor/Debugger > Display, and then click Help.
Note This limit is a visual cue only and does not prevent text from exceeding the limit. For information on setting a value to wrap comment text at a specified column number automatically, see Formatting MATLAB Comments in Files. |
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 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.
You can set preferences to enable or disable the ability to expand and collapse the following MATLAB programming constructs:
Help 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
Single program, multiple data (spmd) 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 plus sign (
) and appended with an ellipsis (
) 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 a minus
sign (
).
To open the code used in the images in this section, enter the following in the Command Window:
open(fullfile(matlabroot,'help','techdoc','matlab_env',... 'examples','collatzall.m'))
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 block code, the Editor displays all the function or class code
and just the first line of the help code. The first line of the help
code ends with a commented ellipsis
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 plus sign
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 minus sign
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 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.
For information on the structure of a MATLAB code file, including a description of a function definition line and an H1 line, see Basic Parts of a Program File in the Programming Fundamentals documentation.
You can view code that is currently folded by positioning the
pointer over its ellipsis
. 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 on line 6 of collatzall.m when the collatzplot_new function is folded.

Changing Code Folding Preferences. To change the current code folding settings, select File > Preferences > Editor/Debugger > Code Folding. If needed, click Help for assistance.
Default Code Folding Behavior for Files Created Before MATLAB Version 7.5. By default, the first time you open a MATLAB code 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.
Reopening a File After Collapsing Code Folds. Constructs that are collapsed when you close a file remain collapsed when you reopen the file.
Copying and Pasting Folded Code. If you copy a collapsed construct from one region of a file and paste it in another region, the construct is expanded in the pasted location.
Printing 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.
Behavior When Functions 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:
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

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.


You can simultaneously display two different parts of a file in the Editor by splitting the screen display, as shown in the image that follows. This feature makes it easy to compare different lines in a file or to copy and paste from one part of a file to another.
See also Positioning Documents for instructions on displaying multiple documents simultaneously.

The following table describes the various ways you can split the Editor and manipulate the split-screen views. When you open a document, it opens unsplit, regardless of its split status it had when you closed it.
| Operation | Instructions |
|---|---|
Split the screen horizontally. | Do either of the following
|
Split the screen vertically. | Do either of the following:
|
Specify the active view. | Do either of the following:
Updates you make to the document in the active view are also visible in the other view. |
Resize the views. | Drag the splitter. |
Remove the splitter | Do one of the following:
|

![]() | Entering Statements in the Editor | Navigating an Open File in the Editor | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |