Improve Code Readability

Indenting Code

Indenting code makes reading statements such as while loops easier. To set and apply indenting preferences to code in the Editor:

  1. On the Home tab, in the Environment section, click Preferences.

    The Preferences dialog box opens.

  2. Select MATLAB > Editor/Debugger > Language.

  3. Choose a computer language from the Language drop-down list.

  4. In the Indenting section, select or clear Apply smart indenting while typing, depending on whether you want indenting applied automatically, as you type. Smart indenting is always applied automatically in the Live Editor.

    If you clear this option, you can manually apply indenting by selecting the lines in the Editor to indent, right-clicking, and then selecting Smart Indent from the context menu.

  5. Do one of the following:

    • If you chose any language other than MATLAB in step 2, click OK.

    • If you chose MATLAB in step 2, select a Function indenting format, and then click OK. Function indent formats are:

      • Classic — The Editor aligns the function code with the function declaration.

      • Indent nested functions — The Editor indents the function code within a nested function.

      • Indent all functions — The Editor indents the function code for both main and nested functions.

    This image illustrates the function indenting formats.

Note

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

Regardless of whether you apply indenting automatically or manually, you can move selected lines further to the left or right, by doing one of the following:

  • On the Editor tab, in the Edit section, click , , or . For live scripts and functions, this functionality is available on the Live Editor tab, in the Code section.

  • Pressing the Tab key or the Shift+Tab key, respectively.

    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.

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 or in MATLAB® Online™.

To hide, or change the appearance of the vertical line:

  1. On the Home tab, in the Environment section, click Preferences.

    The Preferences dialog box opens.

  2. Select MATLAB > Editor/Debugger > Display.

  3. Adjust the settings in the Right-hand text limit section.

Note

This limit is a visual cue only and does not prevent text from exceeding the limit. To wrap comment text at a specified column number automatically, adjust the settings in the Comment formatting section under MATLAB > Editor/Debugger > Language in the Preferences dialog box.

Code Folding — Expand and Collapse Code Constructs

Code folding is the ability to expand and collapse certain MATLAB programming constructs. This improves readability when a file contains numerous functions or other blocks of code that you want to hide when you are not currently working with that part of the file. MATLAB programming constructs include:

  • Code sections for running and publishing code

  • Class code

  • for and parfor blocks

  • Function and class help

  • Function code

To see the entire list of constructs, select Editor/Debugger > Code Folding in the Preferences dialog box.

To expand or collapse code, click the plus or minus sign that appears to the left of the construct in the Editor. You also 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, right-click, and then select Code Folding > Expand All or Code Folding > Fold All from the context menu. You also can use the Ctrl+Shift+, (comma) and Ctrl+, (comma) keyboard shortcuts, or the code folding buttons in the View tab.

Note

Code folding is not supported in the Live Editor

View Folded Code in a Tooltip

You can view code that is currently folded by positioning the pointer over its ellipsis . The code appears in a tooltip. Viewing folded code in a tooltip is not supported in MATLAB Online.

The following image shows the tooltip that appears when you place the pointer over the ellipsis on line 23 of lenghtofline.m when a for loop is folded.

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

Code Folding Behavior for Functions that 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. This is because MATLAB does not include trailing white space and comments in a function definition that has no explicit end statement. In MATLAB Online, the Editor does include the line when folding the function.

    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 code section overlaps the function code, then the Editor does not show the fold for the overlapping section.

    The three figures that follow illustrate this behavior. The first two figures, Code Folding Enabled for Function Code Only and Code Folding Enabled for Sections Only illustrate how the code folding appears when you enable it for function code only and then section only, respectively. The last figure, Code Folding Enabled for Both Functions and Sections, illustrates the effects when code folding is enabled for both. Because the fold for section 3 (lines 11–13) overlaps the fold for function foo (lines 4–12), the Editor does not display the fold for section 3.

Code Folding Enabled for Function Code Only

Code Folding Enabled for Sections Only

Code Folding Enabled for Both Functions and Sections

Code Refactoring — Automatically convert selected code to a function

In the Live Editor, you can break large live scripts or functions into smaller pieces by automatically converting selected areas of code into functions or local functions. This is called code refactoring.

To refactor a selected area of code:

  1. Select one or more lines of code.

  2. On the Live Editor tab, in the Code section, click Refactor and 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.

Refactoring is also available in the Editor in MATLAB Online. To refactor a selected area of code in the MATLAB Online Editor, on the Editor tab, in the Edit section, click the code refactoring button . Then, select from the available options.