Appearance of an M-File — Making 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 and History, as well as in the Editor. For more information, see the Command Window documentation for Syntax Highlighting.

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

Automatic Indenting

You can set an indenting preference so that program control entries are automatically indented to make reading loops, such as while/end statements, easier. To do so, select File > Preferences > Editor/Debugger > Language, and select a Language, for example, M. For Indenting for Enter key, select Smart indenting or Block indent, and then click OK. Use No indent instead if you want to indent manually. 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.

Manual Indenting

You can manually apply smart indenting to selected lines—select the lines and then select Smart Indent from the Text menu, or right-click and select it from the context menu. This feature indents lines that start with keyword functions or that follow lines containing certain keyword functions. Smart indenting can help you to follow the code sequence.

To move the current or selected lines further to the left, select Decrease Indent from the Text menu. To move the current or selected lines further to the right, select Increase Indent from the Text menu.

You can also indent a line by pressing the Tab key at the start of a line. Or select a line or group or lines and press the Tab key. Press Shift+Tab to decrease the indent for the selected lines. 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.

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

Function Indenting

If you select the language preference for smart indent, 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 are shown 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 is useful, for example, to help you see where copied text will be inserted when you paste.

To highlight the current line, select File > Preferences > Editor/Debugger > Display, and under General display options, select the check box for Highlight current line. You can also specify the color used to highlight the line.

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

Right-Hand Text Limit

By default, a light red vertical line (rule) appears at column 75 in the Editor, providing a cue as to when a line becomes wider than desired, which is useful if you plan to print the file, for example. You can hide the line or change the column number at which it appears. 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 M-File Constructs

Code folding is the ability to expand and collapse certain M-file programming constructs. This improves readability when an M-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 M-file 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 expand icon ( ) and appended with an ellipsis icon ( ) 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 the collapse icon ( ).

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 code, the Editor displays all the function or class code and just the H1 line of the help code. The H1 line ends with a commented ellipsis icon 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 an M-file, place your cursor anywhere within the M-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 an M-file, including a description of a function definition line and an H1 line, see Basic Parts of an M-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 icon . 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 icon 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

Be aware of the following:

Image of Editor showing unfolded code within a while loop.

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

Split Screen Display

You can simultaneously display two different parts of a file in the Editor. This makes it easy to compare different lines in a file or to copy and paste from one part of a file to another.

Split the screen horizontally by selecting Window > Split Screen > Top/Bottom. Or to split it vertically, select Left/Right.

Alternatively, when there is a scroll bar, split the document into top and bottom views by dragging the splitter bar, as shown in the following illustration, down from above the vertical scroll bar. Similarly, to split into left and right views, drag the splitter bar from the left of the horizontal scroll bar. The pointer assumes a double-headed arrow shape when you position it on the splitter bar.

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

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

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

Resize of the views by dragging the splitter. The pointer assumes an arrow shape when you position it on the splitter.

Only one view is active at any time, meaning, you will see only the cursor in one of the views. To change the active view, select Window > Split Screen > Switch Focus or its keyboard equivalent, which is shown with the menu item. The cursor returns to its last position in that view.

Make changes to the document in either view. Both views of the file are always current, so you see the changes in either view.

You split each open document individually, so there can be multiple views at once. You can split some documents horizontally, others vertically, and leave others unsplit. When you open a document, it always opens unsplit, regardless of its split status when you last had it open.

You can remove a document split using any of these methods:

See also Summary of Actions for Arranging Documents for instructions to display multiple documents simultaneously.

  


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