Contenido principal

Check Syntax as You Type

You can avoid syntax errors using syntax highlighting and delimiter matching.

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 color display is known as syntax highlighting. By default:

  • Keywords are blue.

  • Character vectors and strings 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 macOS 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.

To change syntax highlighting settings, on the Home tab, in the Environment section, click Settings. Then, select Editor/Debugger > MATLAB Language or Editor/Debugger > Other Languages.

Before R2025a: Select MATLAB > Editor/Debugger > Language and from the Language drop-down list, choose a language.

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.

MATLAB indicates matching delimiters in the Editor, Live Editor, and Command Window by briefly underlining both delimiters in the pair.

MATLAB indicates mismatching delimiters in the Editor, Live Editor, and Command Window by briefly crossing out the mismatched delimiter.

If a matching delimiter exists, but it is not visible on the screen, a window opens and displays the line containing the matching delimiter. Click in the window to go to that line.

Window showing the off-screen lines of code that include the closing bracket for the selected line

You can change if and when MATLAB alerts you to matched and mismatched delimiters. On the Home tab, in the Environment section, click Settings. Select MATLAB > Keyboard and in the Delimiter matching section, select from the available options.

See Also

Topics