Skip to Main Content Skip to Search
Product Documentation

Avoid Mistakes When Entering Code

Highlight Syntax to Help Ensure Correct Entries in the Command Window

To help you find MATLAB elements, such as matching if/else statements, some entries appear in different colors in the Command Window. This is known as syntax highlighting. By default:

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

To change syntax highlighting preferences, select File > Preferences > Editor/Debugger > Languages.

Avoid Mismatched Parentheses, Brackets, Braces, and Paired Keywords

When you type code at the Command prompt or in the Editor, MATLABhelps you avoid mismatched delimiters, such as parenthesis, brackets, and braces. In addition, MATLAB indicates paired language keywords in the Editor. Paired language keywords include for, if, while, else, and end statements.

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

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.

Image of pop-up window in the Command Window showing a matching delimiter.

Change Delimiter Matching Indicators

To change delimiter matching indicators, and when and if they appear:

  1. Select File > Preferences > Keyboard.

  2. Under Delimiter Matching, choose when you want MATLAB to notify you of matches and mismatches in pairs of delimiters.

  3. Choose how you want MATLAB to indicate matches by selecting one of the following from the Show match with drop-down menu associated with your selection or selections in step 2:

    • Balance — The corresponding delimiter is highlighted briefly.

    • Underline — Both delimiters in the pair are underlined briefly.

    • Highlight — Both delimiters in the pair are highlighted briefly.

  4. Choose how you want MATLAB to indicate mismatches by selecting one of the following from the Show mismatch with menu associated with your selection or selection sin step 2:

    • Beep — MATLAB beeps.

    • Strikethrough — The delimiter you typed is crossed out briefly.

    • None — There is no action.

Complete Names in the Command Window Using the Tab Key

MATLAB helps you complete the names of functions, models, MATLAB objects, files, folders, variables, structures, and Handle Graphics property names, which can help you to avoid typographical errors.

To complete names in the Command Window:

  1. Select File > Preferences > Keyboard.

  2. Under Tab completion, select Enable in Command Window, and Tab key narrows completions.

  3. Click OK.

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

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

  5. Do one the following, depending on how MATLAB responds in step 3.

    If MATLAB:Then:
    Completes the name you wantYou are done.
    Presents a list of possible matches

    Use the arrow keys to select the name you want, and then press the Tab key. See Example of Name Completion for a Variable in the Command Window.

    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.

    Completes the first part of a name that uses dot notation

    At the command line, type a dot, and then press the Tab key. Repeat until the name is complete.

    See Example of Name Completion for a Class Folder.

    Completes one property of several

    At the command prompt, type a comma and the first part of the next property, and then press the Tab key. Repeat for all properties you want to specify.

    See Example of Name Completion for Handle Graphics Properties.

Example of Name Completion for a Variable in the Command Window

This example shows how to use tab completion for a variable name when there is more than one name that starts with the characters you type. The process is similar for other names that do not use dot notation.

  1. Select File > Preferences > Keyboard.

  2. Under Tab completion, select Enable in Command Window.

  3. Click OK.

  4. In the Command Window, create a variable, costs_march, by typing the following in the Command Window.

    costs_march = 100;
  5. Type cos, and then press the Tab key.

    Image of Command Window showing multiple possible tab completions. By default, the first item in the list is highlighted.

    MATLAB lists all possible completions, including MATLAB functions and the variable name you created, costs_march.

  6. Navigate the list of possible completions using the up and down arrow keys to find and highlight costs_march.

  7. Choose costs_march by pressing the Tab key.

    The Command Window displays costs_march.

  8. At the command prompt, add arguments, and press Enter.

    MATLAB runs the statement.

Example of Name Completion for a Class Folder

This example shows how to complete the name for a class folder. The steps for completing other names that use dot notation are similar.

Consider the containers package in the matlabroot folder. The containers package contains a Map @- folder and a Map.m file. The folder structure appears as follows:

+containers\@Map\Map.m

To use tab completion:

  1. Select File > Preferences > Keyboard.

  2. Under Tab completion, select Enable in Command Window.

  3. Click OK.

  4. In the Command Window, type edit cont, and then press Tab.

    MATLAB displays a list from which to choose, such as the following.

  5. Select containers, and then press Tab.

    The Command Window displays edit containers.

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

    The Command Window displays:

    edit containers.Map

    .

  7. At the command prompt, add a dot after Map, and then press Tab

    MATLAB displays a new list.

  8. Scroll down the list, select isvalid, and then press the Tab key.

    The Command Window displays edit containers.Map.isvalid.

Example of Name Completion for Handle Graphics Properties

This example shows how to complete the names of MATLAB Handle Graphics properties using tab completion. The steps for completing properties for other objects that use dot notation are similar.

  1. Select File > Preferences > Keyboard.

  2. Under Tab completion, select Enable in Command Window.

  3. Click OK.

  4. In the Command Window, create a figure:

    scrsz = get(0,'ScreenSize');
    f=figure('Position',[1 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2])
  5. Type set(f, 'pap , and then press the Tab key.

    MATLAB displays

    Image of possible tab completions for the paper property. The list consists of: PaperOrientation, PaperPosition, PaperPositionMode, PaperSize, PaperType, PaperUnits. By default, the first item, PaperOrientation is highlighted.

  6. Type u, and then press the Tab key.

    MATLAB completes the property, including the closing quote:

    set(f, 'paperunits'
    
  7. Type , 'c, and then press the Tab key.

    Because centimeters is the only possible completion, MATLAB automatically completes the property and displays:

    set(f, 'paperUnits', 'centimeters'
  8. Complete the command by typing a closing parenthesis at the command prompt.

View Function Syntax Hints While Entering a Statement

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. These function hints appear for both MATLAB installed functions and functions you create. The syntax 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.

Be aware that:

To use function syntax hints in the Command Window, follow these steps, as shown for the size function:

  1. Ensure function hints are enabled.

    Select File > Preferences > Keyboard, and then setting the options for Function hints.

  2. Type any output arguments and the equal sign. For example:

    [m]=
  3. Type the function name and the opening (left) parenthesis, and then pause. For example:

    size(

    A yellow window appears, displaying the syntax options for the function.

  4. Type a variable name for the first input argument. You can type a variable for any argument that appears in blue.

      Note   Enter your variable names, and not the argument names shown in the window.

    In this example, for the input argument X, type your variable:

    my_data
  5. Enter more arguments:

    1. Type a comma after the argument you just entered.

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

      For this example, only the X,dim option displays. dim is blue now, because you can enter it.

    2. Type the next input argument. It can be any argument that is blue.

      For example, to run size for the third dimension, enter 3 for dim.

    Continue entering more input arguments in the same way.

  6. When you finish entering arguments, type the closing (right) parenthesis.

    The pop-up window closes.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS