Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Making MATLAB Code Files More Readable

Syntax Highlighting

Some entries appear in different colors to help you better find matching elements, such as if/else statements. Similarly, unterminated strings have a different color than terminated strings. This is called syntax highlighting and is used in the Command Window, Command History, and the Editor. For more information, see the Command Window documentation for Highlighting Syntax to Help Ensure Correct Entries.

When you paste or drag a selection from the Editor to another application, such as Microsoft Word, the pasted text maintains the syntax highlighting colors and font characteristics from the Editor. MATLAB software pastes the selection to the Clipboard in RTF format, which many Microsoft Windows and Macintosh applications support.

Indenting

This section describes how to apply indenting to code in the Editor.

Enabling Automatic Indenting

Set an indenting preference so that program control entries indent automatically to make reading statements such as while loops easier:

  1. Select File > Preferences > Editor/Debugger > Language.

  2. From the Language drop-down menu, select a language.

  3. Under Indenting, select Apply smart indenting while typing.

  4. Click OK.

If you want to indent manually, clear Apply smart indenting while typing in step 3. For more information about indenting preferences, click Help in the Preferences dialog box. Specify the indenting size and other options by selecting File > Preferences > Editor/Debugger > Tab.

Manually Applying Indenting

Indenting can help you to read the code sequence. Manually apply smart indenting, so that lines indent if they start with a keyword function or if they follow lines containing certain keyword functions:

  1. Select the lines to indent.

  2. Select Text > Smart Indent or right-click and select Smart Indent from the context menu.

  3. Adjust the text by doing one of the following:

    • Move the current or selected lines further to the left, by selecting Text > Decrease Indent.

    • Move the current or selected lines further to the right, by selecting Text > Increase Indent.

Additional methods for indenting a line are as follows:

For more information about manual indenting, select File > Preferences > Editor/Debugger > Tab, and then click Help.

Function Indenting

You can select from three indenting options when you enter a subfunction or a nested function (a function within a function) in the Editor. For details, see Function Indenting Format.

Line and Column Numbers

Line numbers display along the left side of the Editor window. You can elect not to show the line numbers using preferences. For details, select File > Preferences > Editor/Debugger > Display, and then click Help.

The line and column numbers for the current cursor position display in the far right side of the status bar in the Editor.

Highlight Current Line

You can set a preference to highlight the current line, that is the line with the caret (also called the cursor). This helps you see where copied text will be inserted when you paste, for example.

To highlight the current line:

  1. Select File > Preferences > Editor/Debugger > Display.

  2. Under General display options, select Highlight current line.

  3. If you want a highlight color other than green, click the color palette and choose a different color.

In the following image, the current line is highlighted in the default highlighting color—green.

Image of Editor showing that the line where cursor is located is highlighted in green.

Right-Side Text Limit

By default, a light gray vertical line (rule) appears at column 75 in the Editor, indicating where a line becomes wider than desired. This is useful, for example, if you want to keep each line below a limit imposed by another text editor in which you intend to view the code. You can change the width and color of the vertical line, as well as the column number at which the vertical line appears. If you want, you can hide the vertical line. For more information, select File > Preferences > Editor/Debugger > Display, and then click Help.

Class, Function, or Subfunction

The right side of the Editor status bar shows the class, function, or subfunction where the cursor is currently placed, depending on the type of file you are viewing, as follows:

Code Folding — Expanding and Collapsing File Constructs

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

You can set preferences to enable or disable the ability to expand and collapse the following MATLAB programming constructs:

By default, code folding is enabled for all programming constructs except if/else blocks and switch/case blocks. Select File > Preferences > Editor/Debugger > Code Folding, and then click Help for details on setting preferences.

When you fold a construct, all the code associated with that construct is collapsed such that the Editor displays only the first line of the construct prepended by the plus sign ( ) and appended with an ellipsis ( ) to indicate there is more code. When you expand a construct, all the code associated with that construct appears and the first line of the construct is prepended with a minus sign ( ).

To open the code used in the images in this section, enter the following in the Command Window:

open(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','collatzall.m'))

The following image shows the collatzall and collatzplot_new functions collapsed and the collatz function code expanded.

Image of Editor showing collatzall and collatzplot_new functions collapsed and the collatz function expanded to reveal the code between the function declaration and the end statement.

When you expand a function or class, but collapse its associated help block code, the Editor displays all the function or class code and just the first line of the help code. The first line of the help code ends with a commented ellipsis to indicate there is additional help code, as shown in the following image.

Image of Editor with the third function expanded, but its associated help code collapsed.

To expand code for a construct that is currently collapsed, do one of the following:

To collapse code for a construct that is currently expanded, do one of the following:

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 > Collapse All from the context menu.

For information on the structure of a MATLAB code file, including a description of a function definition line and an H1 line, see Basic Parts of a Program File in the Programming Fundamentals documentation.

Viewing 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. This lets you quickly view the code without unfolding it.

The following image shows the tooltip that appears when you place the pointer over the ellipsis on line 6 of collatzall.m when the collatzplot_new function is folded.

Image of Editor with Tooltip displaying code within the folded function.

Code Folding Behavior and Preferences

Changing Code Folding Preferences.  To change the current code folding settings, select File > Preferences > Editor/Debugger > Code Folding. If needed, click Help for assistance.

Default Code Folding Behavior for Files Created Before MATLAB Version 7.5.  By default, the first time you open a MATLAB code file that existed before MATLAB Version 7.5 (R2007b) using MATLAB Version 7.5 (R2007b) or later, code folding is enabled and all constructs are expanded.

Reopening a File After Collapsing Code Folds.  Constructs that are collapsed when you close a file remain collapsed when you reopen the file.

Copying and Pasting Folded Code.  If you copy a collapsed construct from one region of a file and paste it in another region, the construct is expanded in the pasted location.

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

Behavior When Functions 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:

Code Folding Enabled for Function Code Only

Code Folding Enabled for Cells Only

Code Folding Enabled for Both Functions and Cells

Effect of Syntax Errors on Code Folding.  If your code contains syntax errors, the code folding indicators might appear to be in the wrong location. For example, suppose your code currently appears as shown in the first figure that follows. If you delete the while statement, it introduces a syntax error at line 3, as shown in the second figure that follows. Notice that the minus sign remains in the same location it held for the syntactically correct code. After you correct the syntax error, the Editor adjusts and displays the code folding indicators appropriately.

Image of Editor showing unfolded code within a while loop.

Image of Editor showing that the minus sign that was in place for the while loop, remains on the same line number after the while statement is deleted.

Displaying Two Parts of a File Simultaneously

You can simultaneously display two different parts of a file in the Editor by splitting the screen display, as shown in the image that follows. This feature makes it easy to compare different lines in a file or to copy and paste from one part of a file to another.

See also Positioning Documents for instructions on displaying multiple documents simultaneously.

Image of Editor showing a horizontal splitter. Double-click the splitter to remove the split. Drag the splitter to resize the views.

Splitting the Screen Display

The following table describes the various ways you can split the Editor and manipulate the split-screen views. When you open a document, it opens unsplit, regardless of its split status it had when you closed it.

OperationInstructions

Split the screen horizontally.

Do either of the following

  • Select Window > Split Screen > Top/Bottom.

  • If there is a vertical scroll bar, as shown in the illustration that follows, drag the splitter bar down.

Split the screen vertically.

Do either of the following:

  • Select Window > Split Screen > Left/Right.

  • If there is a horizontal scroll bar, as shown in the illustration that follows, drag the splitter bar from the left of the scroll bar.

Specify the active view.

Do either of the following:

  • Select Window > Split Screen > Switch Focus.

  • Click in the view you want to make active.

Updates you make to the document in the active view are also visible in the other view.

Resize the views.

Drag the splitter.

Remove the splitter

Do one of the following:

  • Drag the splitter to an edge of the window.

  • Double-click the splitter.

  • Select Window > Split > Screen > Off.

Image of Editor showing the splitters as described in the text preceding the image.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS