Documentation Center

  • Trial Software
  • Product Updates
我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

Check Syntax As You Type

Syntax Highlighting

To help you identify MATLAB® elements, some entries appear in different colors in the Command Window. This is known as syntax highlighting. By default:

  • Keywords are blue.

  • Strings are purple.

  • Unterminated strings are maroon.

  • Comments are green.

if A > B
'greater'
elseif A < B
'less'
end

Except for errors, output in the Command Window does not appear with 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.

You can change syntax highlighting preferences. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Languages.

Delimiter Matching

MATLAB indicates matched and mismatched delimiters, such as parentheses, brackets, and braces, to help you avoid syntax errors. MATLAB also indicates paired language keywords, such as for, if, while, else, and end statements.

By default, MATLAB indicates matched and mismatched delimiters and paired language keywords as follows:

  • Type a closing delimiter—MATLAB briefly highlights the corresponding opening delimiter.

  • Type more closing delimiters than opening delimiters—MATLAB beeps.

  • Use the arrow keys to move the cursor over one delimiter—MATLAB briefly underlines both delimiters in a pair. If no corresponding delimiter exists, MATLAB puts a strike line through the unmatched delimiter.

If a matching delimiter exists, but it is not visible on the screen, a pop-up window appears and shows the line containing the matching delimiter. Click in the pop-up window to go to that line.

You can change delimiter matching indicators, and when and if they appear. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Keyboard.

Tab Completion

MATLAB can help you avoid typographical errors by completing the names of functions, models, MATLAB objects, files, folders, variables, structures, and Handle Graphics® property names.

To complete names in the Command Window, type the first few characters of the name you want to complete, and then press the Tab key.

If MATLAB presents a list of possible matches, use the arrow keys to select the name you want, and then press the Tab key.

In addition, you can:

  • Clear the list without selecting anything, by pressing the Esc (escape) key.

  • Narrow a long list before making a selection, by adding additional characters to your original term.

  • Complete parts of a name that uses dot notation by adding a dot, and then pressing the Tab key.

  • Complete the names and values of Handle Graphics properties. Begin typing the first part of a property, and then press the Tab key. Type a comma after each property.

For MATLAB to complete a file or folder name, it must be on the search path or in the current folder. Variables and properties must be in the current workspace.

In the Editor, MATLAB completes:

  • Nested functions only when they are available at the current location of the cursor.

  • Names of variables defined in the active document. The variable must be valid at the current location of the cursor (that is, already defined).

In the Editor, MATLAB does not complete:

  • Field names of structure arrays defined only within the active file.

  • Method or property names for objects defined only within the active file.

    Note:   To add spaces within statements using the Tab key in the Editor, first add a space, and then press Tab. Otherwise, when tab completion is enabled, MATLAB attempts to complete a name.

Tab completion is enabled by default. To change this setting, on the Home tab, in the Environment section, click Preferences. Select MATLAB > Keyboard.

Example of Name Completion

This example shows how to complete the name for the containers.Map.keys method.

  1. In the Command Window, type help cont, and then press Tab.

    MATLAB displays a list of selections.

  2. Select containers, and then press Tab.

    The Command Window displays help containers.

  3. At the command prompt, add a dot after containers, and then press Tab.

    The Command Window displays:

    help containers.Map
  4. At the command prompt, add a dot after Map, and then press Tab.

    MATLAB displays a new list.

  5. Scroll down the list, select keys, and then press the Tab key.

    The Command Window displays help containers.Map.keys.

Function Syntax Hints

As you enter a function in the Command Window or Editor, syntax hints open in a pop-up window to display allowable input arguments for a function.

Function hints appear for both MATLAB installed functions and functions you create. The syntax hints for MATLAB functions comes from the documentation. The syntax for functions you create comes from the function definition statement (first executable line) in the MATLAB program file. That file must be on the search path or in the current folder.

To use function syntax hints, type a function name with an opening parenthesis, and then pause. A tooltip opens showing the basic syntax for the function.

You can type a variable for any argument that appears in blue. Enter your variable names, and not the argument names shown in the window.

The displayed syntax options change, based on the argument you just entered.

Some function names are overloaded. That is, there are methods with the same name as a function that support different types of inputs. Overloaded methods require that you pass an object as the first input. When you specify the object name, the syntax hints update to reflect the associated method, as shown.

Function syntax hints are suggestions only. Some allowable arguments might not appear, or could be in black text when they should be blue.

Function hints are enabled by default. To change this setting, on the Home tab, in the Environment section, click Preferences. Select MATLAB > Keyboard, and then set the options for Function hints.

Was this topic helpful?