Main Content

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 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 preferences, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Editor/Debugger > Language and from the Language drop-down list, choose a language. To change syntax highlighting preferences 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.

In the Editor and Live Editor, MATLAB indicates matching delimiters by briefly underlining both delimiters in the pair. In the Command Window, matching delimiters are indicated by highlighting instead of underlining. In MATLAB Online, the Command Window behavior matches the Editor and Live Editor behavior.

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 Preferences. Select MATLAB > Keyboard and in the Delimiter matching section, select from the available options.

Code Suggestions and Completions

When you write code in the Command Window, Editor, Live Editor, and App Designer, MATLAB shows suggestions for the names of functions, models, MATLAB objects, files, folders, variables, structures, graphics properties, parameters, and options. In addition, MATLAB shows basic syntax information for functions. You can use these suggestions to write commands faster and avoid typographical errors.

Name Suggestions

In the Editor, Live Editor, and App Designer, name suggestions appear as you type. You also can press Ctrl+Space or the Tab key to show suggestions. MATLAB shows the most relevant name suggestions as you type. To show new and more refined suggestions, continue typing. If there are no name suggestions, MATLAB displays No suggestions.

To insert a suggestion in your code, use the arrow keys to select the name that you want, and then press the Right Arrow, Tab, or Enter key to accept the suggestion. To clear the list of suggestions without selecting anything, press the Esc key.

The typed characters "co" and a list of suggestions

In the Command Window, name suggestions appear only after you press the Tab key. To show suggestions, type the first few characters of the name that you want suggestions for, and then press the Tab key. Use the arrow keys to select the name that you want, and then press the Tab key again to accept the suggestion. In MATLAB Online, the Command Window also shows suggestions as you type and when you press Ctrl+Space or the Tab key.

Name suggestions are useful when completing the names and values of graphics properties. For example, plot(x,y,"LineStyle","-").

Partially completed call to the plot function with a list of suggested values for the LineStyle property

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

Partially completed call to the format function with a list of suggested values for the style argument

MATLAB displays suggestions based on its current state, including:

  • 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 document.

Tip

To insert a tab within a statement when suggestions and completions are enabled in the Editor, Live Editor, and App Designer, first, add a space. Then, press the Tab key. Alternatively, go to the Home tab, and in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Automatic Completions and in the Suggestions and completions section, clear the Use tab to show suggestions (in addition to ctrl+space) option. If there are no available completions, MATLAB always inserts a tab.

In the Command Window, to insert a tab, you must disable showing name suggestions on tab. To disable showing name suggestions, in the Preferences window, select MATLAB > Command Window > Automatic Completions and clear the Enable tab completion option. In the MATLAB Online Command Window, clear the Use tab to show suggestions (in addition to ctrl+space) option.

Function Syntax Suggestions

When you type the name of a function followed by an opening parenthesis, MATLAB shows the basic syntax information for the function. In the Editor, Live Editor, App Designer, and MATLAB Online Command Window, if a function has multiple syntaxes, you can use the drop-down arrow to the right of the syntax to show all available syntaxes. Alternatively, use Ctrl+Down Arrow to show and hide the additional syntaxes. On macOS systems, use the Command key instead of Ctrl.

Partially completed call to the plot function with a list of four alternative syntaxes

You can type an input for any argument that appears in blue. Enter your own input variables or values, and not the argument names shown in the suggestion. The displayed syntax options change based on the arguments you enter.

In the Editor, Live Editor, App Designer, and MATLAB Online Command Window, MATLAB shows descriptions for each argument. Optional arguments are indicated by the (Optional) keyword next to the argument description. If MATLAB suggests multiple arguments, click the arrow to the right of the argument description to view the additional arguments. Alternatively, you can use the Ctrl+Alt+Right Arrow and Ctrl+Alt+Left Arrow keyboard shortcuts.

Partially completed call to the plot function with the description of the first of two suggested arguments displayed and a right arrow next to the argument description that can be used to view the second suggested argument description

To open the documentation for the function MATLAB is showing syntax suggestions for, click the Help button to the left of the function syntax suggestions, or, in the Command Window, click the More Help... link below the function syntax suggestions.

Suggestions for function syntaxes and arguments appear for MATLAB installed functions and for functions that you create. The suggestions for MATLAB functions come from the documentation. The suggestions for functions that 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 that you create, see Add Help for Your Program and Customize Code Suggestions and Completions.

Disable Suggestions and Completions

In the Editor, Live Editor, and App Designer, you can disable showing suggestions automatically. You also can disable the use of the Tab key for showing suggestions and the use of the Right Arrow key for accepting suggestions. To disable these options, go to the Home tab, and in the Environment section, click Preferences. Select MATLAB > Editor/Debugger > Automatic Completions and in the Suggestions and completions section, clear one or more of the options:

  • To disable showing suggestions automatically, clear the Show suggestions automatically option.

  • To disable showing suggestions after you press the Tab key, clear the Use tab to show suggestions (in addition to ctrl+space) option. When this option is cleared, you can still show suggestions by pressing Ctrl+Space.

  • To disable accepting a suggestion using the Right Arrow key, clear the Use right arrow to accept suggestions (in addition to tab and enter) option. When this option is cleared, you can still accept a suggestion using the Tab and Enter keys.

In the Command Window, you can disable showing name suggestions and function syntax suggestions. To disable showing name suggestions, in the Preferences window, select MATLAB > Command Window > Automatic Completions and clear the Enable tab completion option. To disable showing function syntax suggestions, clear the Enable function hints option. To disable reducing the list of suggestions as you type each additional character and press the Tab key, clear the Tab key narrows completions option.

In MATLAB Online, the Command Window behavior matches the Editor, Live Editor, and App Designer behavior. To disable showing suggestions automatically, using the Tab key for showing suggestions, and using the Right Arrow key for accepting suggestions, in the Preferences window, select MATLAB > Command Window > Automatic Completions. Then, in the Suggestions and completions section, clear one or more of the options.

Related Topics