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

To change syntax highlighting preferences, on the Home tab, in the Environment section, select 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.

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, MATLAB suggests and completes the names of functions, models, MATLAB objects, files, folders, variables, structures, graphics properties, parameters, and options. Use these automatic suggestions and completions to write commands faster and avoid typographical errors.

The Editor and Live Editor display possible matches as you type. To insert a match in your script, use the arrow keys to select the name that you want, and then press the Tab key. Press the Esc key to clear the list of possible completions without selecting anything.

The Command Window displays possible matches only after you press the Tab key. To complete names in the Command Window, type the first few characters of the name that you want to complete, and then press the Tab key. Use the arrow keys to select the name that you want, and then press the Tab key again. In MATLAB Online, the Command Window behavior matches the Editor and Live Editor behavior, displaying possible matches as you type.

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. Alternatively, press 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. Alternatively, you can use the Ctrl + Alt + Right Arrow and Ctrl + Alt + Left Arrow keyboard shortcuts.

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

Disable Automatic Suggestions and Completions

In the Editor and Live Editor, for MATLAB to suggest and complete names only after you press the Tab key, on the View tab, in the Display section, click the Automatic Completions button off. Or, you can go to the Home tab, and in the Environment section, click Preferences. Then, select Editor/Debugger > Automatic Completions and in the Suggestions and completions section, select Show on tab.

In the Command Window, to disable suggestions and completions, in the Preferences window, select MATLAB > Command Window > Automatic Completions and clear the Enable tab completion option.

In the Command Window in MATLAB Online, for MATLAB to suggest and complete names only after you press the Tab key, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Command Window > Automatic Completions and in the Suggestions and completions section, select Show on tab.

Function Syntax Hints

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

Function syntax hints appear for MATLAB installed functions and for functions that you create. The syntax hints for MATLAB functions come from the documentation. The syntax hints 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.

To use function syntax hints, type a function name with an opening parenthesis. A window opens showing the basic syntax for the function. In the Editor and Live 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 that you just entered.

In the Editor and Live 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 are updated to reflect the correct version of the function, as shown. The Live Editor might not display all overloads.

Function syntax hints are only suggestions. 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, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Command Window > Automatic Completions and in the Suggestions and completions section, clear Enable function hints. Disabling function syntax hints is not supported for the Editor, Live Editor, or for the Command Window in MATLAB Online.