Edit and Format Code
To edit you code, in the Editor and Live Editor, you can use column selection, code autocompletion, and refactoring. To format your code and make your code easier to read, use indentation, text-width indication, and code folding.
When adding or editing code in the Editor and Live Editor, you can select and edit a rectangular area of code (also known as column selection or block edit). If you want to copy or delete several columns of data (as opposed to rows) or if you want to edit multiple lines at one time, selecting and editing code is useful. To select a rectangular area, press the Alt key while making a selection with the mouse. On macOS systems, use the Option key instead.
For example, select the second column of data in
0 to set all the selected values to 0.
Before R2021b, column selection is available only in the Live Editor, not in the Editor.
You can change the case of selected text or code from all uppercase to lowercase, or vice versa, in the Editor and Live Editor. Select the text, right-click, and select Change Case. Alternatively, you can press Ctrl+Shift+A. If the text contains uppercase and lowercase text, MATLAB® changes the case to all uppercase.
Before R2021b, the Change Case option is available only in the Live Editor, not in the Editor.
Automatically Complete Code
MATLAB automatically completes parentheses and quotes when entering code in
the Editor and Live Editor. For example, if you type an open parenthesis in the
Editor or Live Editor, MATLAB adds the closing parenthesis. MATLAB also automatically splits comments, character vectors, strings, and
parentheses when you press Enter. For example, if you press
Enter in a comment, MATLAB moves the text after the cursor to a new line and adds a percent
%) symbol to the beginning of the new line.
MATLAB can also automatically complete block endings in control flow statements and function and class definitions. To autocomplete block endings, on the Home tab, in the Environment section, click Preferences. Select Editor/Debugger > Automatic Completions and in the Autocoding options section, select one or more of the Autocomplete block endings options.
To undo an automatic code completion, press Ctrl+Z or the Undo button. To disable automatic code completions, in the Editor/Debugger > Automatic Completions preferences, clear one or more of the options in the Autocoding options section. For more information, see Editor/Debugger Preferences.
Before R2021b, MATLAB completes code only in the Live Editor, not in the Editor.
You can break large scripts or functions into smaller pieces by converting selected areas of code into functions or local functions, known as code refactoring.
To refactor a selected area of code:
Select one or more lines of code.
On the Editor or Live Editor tab, in the Code section, click Refactor and select from the available options.
Enter a name for the new function. MATLAB creates a function with the selected code and replaces the original code with a call to the newly created function.
Before R2021b, refactoring options are available only in the Live Editor, not in the Editor.
Indenting code makes functions and statements such as
loops easier to read. By default, MATLAB indents code such as functions and the body of loops in the Editor and
Live Editor as you type. When you indent lines by using tabs or spaces, MATLAB also aligns subsequent lines with those lines.
You can enable or disable automatic indenting depending on how you prefer to write code. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Language and in the Language drop-down list, select a programming language. Then, in the Indenting section of the selected language, select or clear the Apply smart indenting while typing option.
Indenting preferences are not supported for TLC, VHDL, or Verilog.
In MATLAB Online™, indenting preferences are located under MATLAB > Editor/Debugger > MATLAB Language and MATLAB > Editor/Debugger > Other Languages.
To indent selected lines of code if automatic indenting is disabled, go to the Editor or Live Editor tab and in the Code section, click the Smart Indent button.
To manually increase the indent of selected lines further to the left or right, on the Editor or Live Editor tab, click the Increase Indent , or Decrease Indent buttons. Manually increasing the indent works whether automatic indenting is enabled or disabled. Alternatively, you can use the Tab key or the Shift+Tab key, respectively. If you select the Emacs-style Tab key smart indenting option in the MATLAB > Editor/Debugger > Tab preferences, the selected lines indent according to indenting practices.
Before R2018a, indenting preferences are supported only in the Editor, not in the Live Editor.
Specify Function Indenting Preferences
You can specify how functions indent in MATLAB code files. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Language and in the Language drop-down list, select MATLAB. Then, select from the Function indenting format options:
Classic— The Editor and Live Editor align the function code with the function declaration.
Indent nested functions— The Editor and Live Editor indent the function code within a nested function.
Indent all functions— The Editor and Live Editor indent the function code for main and nested functions.
In MATLAB Online, MATLAB indenting preferences are located under MATLAB > Editor/Debugger > MATLAB Language.
Code folding expands and collapses blocks of MATLAB code in the Editor. You can use code folding to hide code that you are not currently working on. Code folding improves the readability of a file that contains numerous functions or other blocks of code. Code folding is not supported in the Live Editor.
For example, you can fold:
To expand or collapse a block of code, click the plus or minus sign that appears to the left of the construct in the Editor. Alternatively, you can use the Ctrl+Shift+. (period) and Ctrl+. (period) keyboard shortcuts or use the code folding buttons in the View tab.
To expand or collapse all of the code in a file, place your cursor anywhere within the file, go to the View tab, and select the Expand All or Collapse All buttons. Alternatively, you can use the Ctrl+Shift+, (comma) and Ctrl+, (comma) keyboard shortcuts.
If you print a file with one or more collapsed constructs, those constructs are expanded in the printed version of the file.
You can change which programming constructs can be folded and whether a programming construct is collapsed the first time that you open a MATLAB file. On the Home tab, in the Environment section, click Preferences. Select Editor/Debugger > Code Folding, and then adjust the preference options.
Change the Right-Side Text Limit Indicator
By default, a light gray vertical line (rule) appears at column 75 in the Editor, indicating where a line exceeds 75 characters. You can set this text limit indicator to another value, which is useful, for example, if you want to view the code in another text editor that has a different line width limit. The right-side text limit indicator is not supported in the Live Editor.
To hide or change the appearance of the vertical line:
On the Home tab, in the Environment section, click Preferences.
In the Preferences window, select MATLAB > Editor/Debugger > Display.
Adjust the settings in the Right-hand text limit section.
The right-side text limit indicator is a visual cue only and does not prevent text from exceeding the limit. To wrap comment text at a specified column number automatically, go to the Home tab and in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Language, and adjust the Comment formatting preferences. To adjust Comment formatting preferences in MATLAB Online, select Editor/Debugger > MATLAB Language.