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.

    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.

    The following image illustrates the function indenting formats.

    Note:   Indenting preferences are not available 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 .

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

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.

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.

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.

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. 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 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 Cells 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 Cells, 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 Cells Only

Code Folding Enabled for Both Functions and Cells

Was this topic helpful?