| MATLAB® | ![]() |
| On this page… |
|---|
Use Command Window Features in the Editor |
After opening an existing file or creating a new file in the Editor, enter statements in the file. Follow the same rules you would use for entering statements in the Command Window as described in Running Functions — Command Window and History:
Also see Getting Help on Selection for Functions, and the Editor features described in the remainder of this section.
To change the case of text in the Editor, select the text. Then, from the Text menu, select one of the following:
Change to Upper Case to change all text to uppercase
Change to Lower Case to change all text to lowercase
Reverse Case to change the case of each letter
This is useful, for example, when copying syntax from help in an M-file, where function and variable names are distinguished by the use of uppercase. But because of that, the code will not run in the MATLAB® Editor or Command Window. In this example, the text was copied and pasted from the output of help get.
V = GET(H, 'Default')
Select all of the text. Select Text > Change to Lower Case. The text becomes
v = get(h, 'default')
If instead you select Reverse Case for
V = GET(H, 'Default')
the case changes to
v = get(h, 'dEFAULT')
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.
Comments in an M-file are strings or statements that do not execute. Add comments in an M-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 M-file runs without those lines. These topics provide details:
You can comment the current line or a selection of lines in an M-file:
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.
Select Comment from the Text menu, or right-click and select it from the context menu.
A comment symbol, %, is added at the start of each selected line, and the color of the text becomes green or the color specified for comments—see Syntax Highlighting.
To uncomment the current line or a selected group of lines, select Uncomment from the Text menu, or right-click and select it from the context menu.

For Java™ and C/C++ files, selecting Text > Comment adds the // symbols at the front of the selected lines. Similarly, Text > Uncomment removes the // symbols from the front of selected lines in Java and C/C++ files.
You can make any line in an M-file 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.

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 not contain any other text. After typing the opening block comment symbol, %{, all subsequent lines assume the syntax highlighting color for comments until you type the closing block comment symbol, %}. Remove the block comment symbols, %{ and %}, to uncomment the lines.
This examples shows some lines of code commented out. When you run the M-file, the commented lines will not execute. This is useful when you want to identify the section of a file that is not working as expected.

You can easily extend a block comment without losing the original block comment, that is, create a nested block comment, as shown in the following example.

To comment out the end of a statement in an M-file, 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, use the ellipsis (...). MATLAB ignores any text appearing after the ... 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 .... 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)
MATLAB ignores the text following the ... on the line
![]()
Note that Middle Initial is green, which is the syntax highlighting color for a comment.
MATLAB continues processing the statement with the next line
![]()
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)
To make comment lines in M-files wrap when they reach a certain column:
Specify the maximum column number using preferences for the Editor. Select Language > M. For Comment formatting, set the Max width.
Select contiguous comment lines that you want to limit to the specified maximum width.
Select Text > Wrap Selected Comments.
The selected comment lines are reformatted so that no comment line in the selected area is longer than the maximum. Lines that were shorter than the specified maximum are merged to make longer lines if they are at the same level of indentation.
To automatically limit comment lines to the maximum width while you type, select the Comment formatting preference to Autowrap comments.
For example, assume you select Autowrap comments and set the maximum width to be 75 characters, which is the width that will fit on a printed page using the default font for the Editor. When typing a comment line, as you reach the 75th column, the comment automatically continues on the next line.
The Editor helps you automatically complete the names of these items as you type them in an M-file:
Functions or models on the search path or in the current directory
Variables, including structures, in the current workspace, where the current workspace is shown in the Stack on the toolbar.
Handle Graphics® properties for figures in the current workspace
Type the first few characters of the item name and then press the Tab key. To use tab completion, you must have the tab completion preference for the Editor selected. For details, see Keyboard Preferences.
Tab completion is also available in the Command Window. There are a few minor differences in how tab completion works in the Command Window, the most notable being that Command Window tab completion supports the completion of file names, whereas the Editor tab completion does not.
Note Tab completion does not complete the names of variables you define in an M-file, but only those variables in the current workspace. This means that while editing, it only completes the names of variables in the base workspace. While debugging, it only completes the names of variables in the current function workspace. |
These examples demonstrate how to use tab completion:
This example illustrates a basic use for tab completion in the Editor. In an M-file opened in the Editor, type the beginning of a function or model on the MATLAB search path or in the current directory, for example,
horz
and press Tab. The Editor automatically completes the name, which for this example displays the function name
horzcat
Complete the statement, adding any arguments, operators, or options. If the Editor does not complete the name horzcat but instead moves the cursor to the right, you do not have the preference set for tab completion. The Editor also moves the cursor to the right when you try to complete a file name; file name tab completion is not supported in the Editor, but is supported in the Command Window.
You can use tab completion anywhere in the line, not just at the beginning. For example, if you type
a = horz
and press Tab, the Editor completes horzcat.
The Editor also completes the names of variables in the current workspace. For example, if there is a variable costs_march in the currently selected workspace, type cost and press Tab. The Editor completes the variable name costs_march. If the Editor displays No Completions Found, costs_march does not exist in the current workspace.
If there is more than one name that starts with the characters you typed, when you press the Tab key, the Editor displays a list of all names that start with those characters. For example, assume you had created the variable costs_march in the base workspace. In an M-file in the Editor, type
cos
and press Tab. The Editor displays

The resulting list of possible completions includes the variable name you created, costs_march, but also includes functions and models that begin with cos, including cosets from Communications Toolbox™, if it is installed and on the MATLAB search path.
Continue typing to make your entry unique. For example, type the next character, such as t in the example. The Editor selects the first item in the list that matches what you typed, in this case, costs_march. Press Enter (or Return) or Tab to select that item, which completes the name in the M-file. In the example, the Editor displays costs_march at the prompt.
You can navigate the list of possible completions using up and down arrow keys, and Page Up and Page Down keys. You can clear the list without selecting anything by pressing Esc. The list of possible completions might include items that are not valid commands, such as private functions.
You can narrow the list of completions shown by typing a character and then pressing Tab if the Keyboard preference Tab key narrows completions is selected. This is particularly useful for large lists. For example, type cam and press Tab to see the possible completions. There is a scroll bar with the list because there are too many completions to be seen at once.

Type p and press Tab again. The Editor narrows the list, showing only all possible camp completions.

Continue narrowing the list in the same way. For the above example, type o and press Tab to further narrow the list. Press Enter or Return to select an item, which completes the name at the prompt.
For structures that are in the current workspace, after the period separator, press Tab. For example, type
mystruct.
and press Tab to display all fields of mystruct. If you type a structure and include the start of a unique field after the period, pressing Tab completes that structure and field entry.
For example, type
mystruct.n
and press Tab, which completes the entry mystruct.name, where mystruct is in the current workspace and contains no other fields that begin with n.
Complete property names for figures in the current workspace using tab completion, as in this graphics example. Here, f is a figure. Type
set(f, 'pap
and press Tab. The Editor displays

Select a property from the list. For example, type
u
and press Enter. The Editor completes the property, including the closing quote.
set(f, 'paperunits'
Continue adding to the statement, as in this example,
set(f, 'paperunits', 'c
and press Tab. The Editor automatically completes the property
set(f, 'paperUnits', 'centimeters'
because centimeters is the only possible completion.
If the preference for tab completion is selected, and you want to also use the Tab key to add spacing within your statements, add a space before pressing Tab. For example, to create this statement
if a=mate %test input value
add a space after mate and then press Tab. If you do not include the space, the following happens instead:
if a=material
This is because the tab completion feature automatically causes mate to complete as the material function.
Alternatively, turn off the tab completion preference to use Tab for spacing in the Editor.
![]() | Starting, Customizing, and Closing the Editor | Appearance of an M-File — Making Files More Readable | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |