Main Content

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.

Column Selection

When adding or editing code in the Editor and Live Editor, you can select and edit a rectangular area of code (also known ascolumn selectionorblock 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 theAltkey while making a selection with the mouse. OnmacOSsystems, use theOptionkey instead.

For example, select the second column of data inA.

Type0所有选中的值设置为0。

Before R2021b, column selection is available only in the Live Editor, not in the Editor.

Change Case

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 selectChange Case. Alternatively, you can pressCtrl+Shift+A. If the text contains uppercase and lowercase text, MATLAB®changes the case to all uppercase.

Before R2021b, theChange Caseoption is available only in the Live Editor, not in the Editor.

Automatically Complete Code

MATLAB 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 completes comments, character vectors, strings, and parentheses when split across two lines.

MATLAB can also complete block endings. On theHometab, in theEnvironmentsection, clickPreferences. SelectEditor/Debugger>Automatic Completionsand in theAutocoding optionssection, select one or more of theAutocomplete block endingsoptions.

To undo an automatic code completion, pressCtrl+Zor theUndo按钮。禁用自动代码完成,theEditor/Debugger>Automatic Completionspreferences, clear one or more of the options in theAutocoding optionssection.

Before R2021b, MATLAB completes code only in the Live Editor, not in the Editor.

Refactor Code

You can break large scripts or functions into smaller pieces by converting selected areas of code into functions or local functions, known as coderefactoring.

To refactor a selected area of code:

  1. Select one or more lines of code.

  2. On theEditororLive Editortab, in theCodesection, clickRefactorand select from the available options.

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

Indent Code

Indenting code makes functions and statements such aswhileloops 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 theHometab, in theEnvironmentsection, clickPreferences. SelectMATLAB>Editor/Debugger>Languageand in theLanguagedrop-down list, select a programming language. Then, in theIndentingsection of the selected language, select or clear theApply smart indenting while typingoption.

Note

Indenting preferences are not supported for TLC, VHDL, or Verilog.

InMATLAB Online™, indenting preferences are located underMATLAB>Editor/Debugger>MATLAB LanguageandMATLAB>Editor/Debugger>Other Languages.

To indent selected lines of code if automatic indenting is disabled, go to theEditororLive Editortab and in theCodesection, click.

To manually increase the indent of selected lines further to the left or right, on theEditororLive Editortab, click, or. Manually increasing the indent works whether automatic indenting is enabled or disabled. Alternatively, you can use theTabkey or theShift+Tabkey, respectively. If you select theEmacs-style Tab key smart indentingoption in theMATLAB>Editor/Debugger>Tabpreferences, 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 theHometab, in theEnvironmentsection, clickPreferences. SelectMATLAB>Editor/Debugger>Languageand in theLanguagedrop-down list, selectMATLAB. Then, select from theFunction indenting formatoptions:

  • Classic— The Editor and Live Editor align the function code with the function declaration.

    Function containing code and a nested function, with the code for each function aligned to the function declaration.

  • Indent nested functions— The Editor and Live Editor indent the function code within a nested function.

    Function containing code and a nested function, with the code in the nested function indented from the nested function declaration.

  • Indent all functions— The Editor and Live Editor indent the function code for main and nested functions.

    Function containing code and a nested function, with the code for each function indented from their function declaration.

InMATLAB Online, MATLAB indenting preferences are located underMATLAB>Editor/Debugger>MATLAB Language.

Fold Code

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:

  • Code sections

  • forandparforblocks

  • Function code

  • Class code

  • Multiline comments

To expand or collapse a block of code, click the plusor minus signthat appears to the left of the construct in the Editor. Alternatively, you can use theCtrl+Shift+. (period)andCtrl+. (period)keyboard shortcuts or use the code folding buttons in theViewtab.

To expand or collapse all of the code in a file, place your cursor anywhere within the file, go to theViewtab, and selectExpand AllorCollapse All. Alternatively, you can use theCtrl+Shift+, (comma)andCtrl+, (comma)keyboard shortcuts.

Note

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 theHometab, in theEnvironmentsection, clickPreferences. SelectEditor/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:

  1. On theHometab, in theEnvironmentsection, clickPreferences.

  2. In the Preferences window, selectMATLAB>Editor/Debugger>Display.

  3. Adjust the settings in theRight-hand text limitsection.

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 theHometab and in theEnvironmentsection, clickPreferences. SelectMATLAB>Editor/Debugger>Language, and adjust theComment formattingpreferences. To adjustComment formattingpreferences inMATLAB Online, selectEditor/Debugger>MATLAB Language.

Related Topics