Check Syntax as You Type

Syntax Highlighting

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

  • Keywords are blue.

  • Character vectors are purple.

  • Unterminated character vectors are maroon.

  • Comments are green.

% check to see if A is greater than B
if A > B
'greater'
elseif A < B
'less'
end

Except for errors, output in the Command Window does not appear with syntax highlighting.

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

You can change syntax highlighting preferences. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Language. In MATLAB Online™, select Editor/Debugger > MATLAB Language or Editor/Debugger > Other 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 or opening delimiter — MATLAB briefly highlights the corresponding opening or closing delimiter.

  • Type more closing delimiters than opening delimiters (or more opening delimiters than closing delimiters) — MATLAB puts a strikethrough on the unmatched delimiter.

  • Use the arrow keys to move the cursor over one delimiter — MATLAB briefly highlights both delimiters in a pair. If no corresponding delimiter exists, MATLAB puts a strikethrough on 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 what the delimiter matching indicators are, and when or if they appear. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Keyboard.

In the Live Editor and MATLAB Online, changing the delimiter matching indicator is not supported. When delimiter matching is enabled, MATLAB indicates matching delimiters by underlining both delimiters in the pair briefly. Mismatching delimiters are indicated by crossing out the mismatched delimiter briefly.

Code Suggestions and Completions

When you write code, MATLAB automatically suggests and completes the names of functions, models, MATLAB objects, files, folders, variables, structures, graphics properties, parameters, and options. You can use automatic suggestions and completions to write commands faster and avoid typographical errors.

The Live Editor automatically displays possible matches as you type. To insert a match in your script, use the arrow keys to select the name you want, and then press the Tab key. Press the Esc key to clear the list of possible completions without selecting anything. To complete names in the Command Window or Editor, type the first few characters of the name you want to complete, and then press the Tab key. Use the arrow keys to select the name you want, and then press the Tab key again.

Automatic suggestions and completions are especially useful when completing the names and values of graphics properties. For example, plot(x,y,'LineStyle','-').

You can also use the suggestions to complete parameter names and options for certain functions. For example, format('shortEng').

When entering a statement that includes a function, MATLAB might suggest multiple syntaxes for the function. To view the additional syntaxes, click the arrow to the right of the syntax. You also can use the Ctrl + Down Arrow keyboard shortcut to show and hide the additional syntaxes. On macOS systems, use the Command key instead of Ctrl.

Similarly, if MATLAB suggests multiple arguments, click the arrow to the right of the argument description to view the additional arguments. You also can use the Ctrl + Alt + Right Arrow and Ctrl + Alt + Left Arrow keyboard shortcuts.

MATLAB displays suggestions based on its current state. This includes:

  • Variables that are defined in the current workspace

  • Files and folders that are on the search path or in the current folder

  • Variables, functions, class properties, and class methods that are defined in the active document and available at the current location of the cursor

MATLAB does not complete field names of structure arrays defined only within the active file.

Tip

To insert a tab within a statement when suggestions and completions are enabled, first, add a space. Then, press the Tab key. Otherwise, MATLAB attempts to complete a name. In the Live Editor, if suggestions and completions are enabled but there are no available completions, MATLAB inserts a tab.

To have MATLAB suggest and complete names only after you press the Tab key in the Live Editor, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Keyboard and in the Suggestions and completions > Live Editor section, select Show on tab.

To disable suggestions and completions in the Command Window and Editor, in the Suggestions and completions section, clear Enable tab completion for the desired tool.

MATLAB Online

In MATLAB Online, the Editor and Command Window automatically display possible matches as you type. This behavior matches the Live Editor behavior.

To change how MATLAB suggests and completes names in MATLAB Online for the Editor, Live Editor, and App Designer, go to the MATLAB > Editor/Debugger preferences and select from the available options. To change how MATLAB suggests and completes names in the Command Window, go to the MATLAB > Command Window preferences.

Function Syntax Hints

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

Function syntax hints appear for both MATLAB installed functions and functions you create. The syntax hints for MATLAB functions come from the documentation. The syntax hints for functions you create come 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. For more information about adding help to the functions you create, see Add Help for Your Program.

To use function syntax hints, type a function name with an opening parenthesis. A pop-up window opens showing the basic syntax for the function. In the Live Editor (and Editor and Command Window in MATLAB Online), you can use the drop-down arrow to the right of the syntax to display all available syntaxes.

You can type an input for any argument that appears in blue. Enter your own input names, and not the argument names shown in the window. The displayed syntax options change based on the argument you just entered.

In the Live Editor (and Editor and Command Window in MATLAB Online), MATLAB displays descriptions for each argument. Optional arguments are indicated by the (optional) keyword next to the argument description.

Some function names are overloaded. That is, there are different functions with the same name that support different types of input. Overloaded functions require that you pass an object as the first input. When you specify the object name, the syntax hints update to reflect the correct version of the function, as shown. The Live Editor might not display all overloads.

Function syntax hints are suggestions only. For detailed information about a function, see the function reference page in the documentation. To open the documentation for the function, click the button to the left of the function syntax hint.

To disable function syntax hints in the Command Window and Editor, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Keyboard and in the Suggestions and completions section, clear Enable function hints for the desired tool. Disabling function syntax hints is not supported in the Live Editor or in MATLAB Online.