Controlling Input

Case and Space Sensitivity

Uppercase and Lowercase for Variables

With respect to case, the MATLAB® language requires an exact match for variable names. For example, if you have a variable a, you cannot refer to that variable as A.

Uppercase and Lowercase for Files and Functions

With respect to functions, filenames, objects, and classes on the search path or in the current directory, MATLAB prefers an exact match with regard to case. MATLAB runs a function if you do not enter the function name using the exact case, but displays a warning the first time you do this.

To avoid ambiguity and warning messages, always match the case exactly. It is a best practice to use lowercase only when running and naming functions. This is especially useful when you use both Microsoft® Windows® and The Open Group UNIX® platforms because their file systems behave differently with regard to case.

Note that if you use the help function, function names are shown in all uppercase, for example, PLOT, solely to distinguish them. Some functions for interfacing toSun Microsystems™ Java™ software do use mixed case and the M-file help and documentation accurately reflect that.

Examples.   The directory first is at the top of the search path and contains the file A.m. If you type a instead of A, MATLAB runs A.m but issues a warning. When you type a again during that session, MATLAB runs A.m but does not show the warning.

Add the directory second after first on the search path, with the file a.m in second. The directory first contains A.m, while second contains a.m Type a. MATLAB runs a.m but displays a warning the first time you do this.

Spaces in Expressions

Blank spaces around operators such as -, :, and ( ), are optional, but they can improve readability. For example, MATLAB interprets the following statements the same way.

y = sin (3 * pi) / 2
y=sin(3*pi)/2

Syntax Highlighting

Some entries appear in different colors to help you better find elements, such as matching if/else statements. This is known as syntax highlighting. You can change the colors using preferences. Note that output does not appear with syntax highlighting, except for errors. For more information, see Colors Preferences for Desktop Tools.

Image of Command Window showing example of syntax highlighting with the default colors.

Matching Delimiters (Parentheses)

You can set a preference for MATLAB to notify you about matched and unmatched delimiters. For example, when you type a parenthesis, bracket, or brace, MATLAB highlights the matched delimiter in the pair. To set these preferences, select File > Preferences > Keyboard > Delimiter Matching. This feature is also available in the Editor/Debugger.

For more information, see Delimiter Matching.

Cut, Copy, Paste, and Undo Features

Use the Cut, Copy, Paste, Undo, and Redo features from the Edit menu when working in the Command Window. You can also access some of these features in the context menu for the Command Window.

Undo applies to some of the actions listed in Edit menu. You can undo multiple times in succession until there are no remaining actions to undo. Select Edit > Redo to reverse an undo.

If you use Enter, you cannot edit a line after entering it, even though you have not completed the flow. In that event, use Ctrl+C to end the flow, and then enter the statements again.

Enter Multiple Lines Without Running Them

To enter multiple lines before running any of them, use Shift+Enter or Shift+Return after typing a line. This is useful, for example, when entering a set of statements containing keywords, such as if ... end. The cursor moves down to the next line, which does not show a prompt, where you can type the next line. Continue for more lines. Then press Enter or Return to run all of the lines.

This allows you to edit any of the lines you entered before you pressing Enter or Return.

Entering Multiple Functions in a Line

To enter multiple functions on a single line, separate the functions with a comma ( , ) or semicolon ( ; ). Using the semicolon instead of the comma will suppress the output for the command preceding it. For example, put three functions on one line to build a table of logarithms by typing

format short; x = (1:10)'; logs = [x log10(x)]

and then press Enter or Return. The functions run in left-to-right order.

Entering Long Statements (Line Continuation)

If a statement does not fit on one line, enter three periods (...) , also called dots, stops, or an ellipsis, at the end of the line to indicate it continues on the next line. Then press Enter or Return. Continue typing the statement on the next line. You can repeat the ellipsis to add a line break after each line until you complete the statement. When you finish the statement, press Enter or Return.

For items in single quotation marks, such as strings, you must complete the string in the line on which it was started. For example, completing a string as shown here

headers = ['Author Last Name, Author First Name, ' ...
'Author Middle Initial']

results in

headers =
Author Last Name, Author First Name, Author Middle Initial

MATLAB produces an error when you do not complete the string, as shown here:

headers = ['Author Last Name, Author First Name, ...
Author Middle Initial']

??? headers = ['Author Last Name, Author First Name, ...
Error: Missing variable or function.

Note that MATLAB ignores anything appearing after the ... on a line, and continues processing on the next line. This effectively creates a comment out of the text following the ... on a line. For more information, see Commenting Out Part of a Statement.

Recalling Previous Lines

Use the arrow, tab, and control keys on your keyboard to recall, edit, and reuse functions you typed earlier. For example, suppose you mistakenly enter

rho = (1+ sqt(5))/2

Because you misspelled sqrt, MATLAB responds with

Undefined function or variable 'sqt'.

Instead of retyping the entire line, press the up arrow key. The previously typed line is redisplayed. Use the left arrow key to move the cursor, add the missing r, and press Enter or Return to run the line. Repeated use of the up arrow key recalls earlier lines, from the current and previous sessions. Using the up arrow key, you can recall any line maintained in the Command History window.

Similarly, specify the first few characters of a line you entered previously and press the up arrow key to recall the previous line. For example, type the letters plo and then press the up arrow key. This displays the last line that started with plo, as in the most recent plot function. Press the up arrow key again to display the next most recent line that began with plo, and so on. Then press Enter or Return to run the line. This feature is case sensitive.

If the up arrow key moves the cursor up but does not recall previous lines, clear the accessibility preference. For more information, see Accessibility.

Another way to view and access commands from the current and previous sessions of MATLAB is with the Command History window — see Command History Window.

Tab Completion in the Command Window

MATLAB helps you automatically complete the names of these items as you type them in the Command Window:

Type the first few characters of the item name and then press the Tab key. To use tab completion, you must have the tab completion preference for the Command Window selected. For details, see Keyboard Preferences.

Tab completion is also available in the Editor/Debugger, but there are some slight differences in usage. See Tab Completion in the Editor.

These examples demonstrate how to use tab completion in the Command Window:

Basic Example — Unique Completion

This example illustrates a basic use for tab completion. After creating a variable, costs_march, type

costs

and press Tab. MATLAB automatically completes the name of the variable, displaying

costs_march

Then complete the statement, adding any arguments, operators, or options, and press Return or Enter to run it. In this example, if you just press Enter, MATLAB displays the contents of costs_march. If MATLAB does not complete the name costs_march but instead moves the cursor to the right, you do not have the preference set for tab completion. If MATLAB displays No Completions Found, costs_march does not exist in the current workspace.

You can use tab completion anywhere in the line, not just at the beginning. For example, if you type

a = cost

and press Tab, MATLAB completes costs_march. You can also select co or position the cursor after co and press Tab to complete costs_march.

Multiple Possible Completions

If there is more than one name that starts with the characters you typed, when you press the Tab key, MATLAB displays a list of all names that start with those characters. For example, type

cos

and press Tab. MATLAB displays

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

The resulting list of possible completions includes the variable name you created, costs_march, but also includes functions that begin with cos, including cosets from the Communications Toolbox™ software, if it is installed on the system and on the search path in MATLAB. MATLAB completes variable names in the currently selected workspace, and the names of functions and models on the search path or in the current directory.

Continue typing to make your entry unique. For example, type the next character, such as t in the example. MATLAB selects the first item in the list that matches what you typed, in this case, costs_march. Press Enter (or Return) or Tab to select that item, which completes the name at the prompt. In the example, MATLAB displays costs_march at the prompt. Add any arguments, and press Enter again to run the statement.

You can navigate the list of possible completions using up and down arrow keys, and Page Up and Page Down keys. You can clear the list without selecting anything by pressing Escape. Note that the list of possible completions might include items that are not valid commands, such as private functions.

Narrowing Completions Shown.   You can narrow the list of completions shown by typing a character and then pressing Tab if the Command Window preference Tab key narrows completions is selected. This is particularly useful for large lists. For example, type cam and press Tab to see the possible completions. There is a scroll bar with the list because there are too many completions to be seen at once.

Image of Command Window showing list of possible completions for cam. List is long and includes a scroll bar, so not all completions are in view.

Type p and press Tab again. MATLAB narrows the list, showing only all possible camp completions.

Image of Command Window showing list of completions for camp.

Continue narrowing the list in the same way. For the above example, type o and press Tab to further narrow the list. Press Enter or Return to select an item, which completes the name at the prompt.

Tab Completion for Directories and Filenames

Tab completion works for directories and filenames in MATLAB functions. For example, type

edit d:/

and press Tab.

MATLAB displays the list of directories and files in d, from which you can choose one. For example, type

mym

and press Tab.

MATLAB displays

edit d:/mymfiles/

where mymfiles is the only directory on your d drive whose name begins with mym. Continue using tab completion to display and complete directory names or filenames until you finish the edit statement.

Tab completion for directories and filenames is not supported for functions you write.

Tab Completion for Structures

For structures in the current workspace, after the period separator, press Tab. For example, type

mystruct.

and press Tab to display all fields of mystruct. If you type a structure and include the start of a unique field after the period, pressing Tab completes that structure and field entry.

For example, type

mystruct.n

and press Tab, which completes the entry mystruct.name, where mystruct contains no other fields that begin with n.

Tab Completion for Properties

Complete property names for figures in the current workspace using tab completion, as in this graphics example. Here, f is a figure. Type

set(f, 'pap

and press Tab. 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.

Select a property from the list. For example, type

u

and press Enter. MATLAB completes the property, including the closing quote.

set(f, 'paperunits'

Continue adding to the statement, as in this example

set(f, 'paperunits', 'c

and press Tab. MATLAB automatically completes the property

set(f, 'paperUnits', 'centimeters'

because centimeters is the only possible completion.

Keyboard Shortcuts in the Command Window

Following is the list of arrow and control keys that serve as shortcuts for using the Command Window. In addition to these shortcut keys (sometimes called hot keys), you can use shortcuts for menu items, which you can view on the menus, as well as general desktop shortcuts described in Keyboard Shortcuts. If you select the Emacs (MATLAB standard) preference for key bindings (see Command Window Key Bindings for an explanation), you can also use the Ctrl+key combinations shown in the table.

Key or Mouse Action for Windows Preference

Control Key for MATLAB standard (Emacs) Preference

Key or Mouse Action for Macintosh Preference

Operation

Up arrow key

Ctrl+P

Up arrow key

Recall previous line — for details, see Recalling Previous Lines. See also Command History Window, which is a log of previously used functions, and Keeping a Session Log.

With the Accessibility preference selected, moves the cursor up a line when it is above the prompt. In that event, use Ctrl+ Up arrow key to recall previous lines for key bindings for MicrosoftWindows and Apple®Macintosh® platforms.

Down arrow key

Ctrl+N

Down arrow key

Recall next line — for details, see Recalling Previous Lines. Works only after using the up arrow or Ctrl+P.

With the Accessibility preference selected, moves the cursor down a line when it is above the prompt. In that event, use Ctrl+ Down arrow key to recall previous lines for key bindings for Windows and Macintosh platforms.

Ctrl+Home

None

Home

Move to top of Command Window.

Ctrl+End

None

End

Move to end of Command Window.

NoneNoneCmd+Home

Move cursor and scroll to top of Command Window.

NoneNoneCmd+End

Move cursor and scroll to end of Command Window.

NoneNoneShift+Cmd+Home

Select to top of Command Window.

NoneNoneShift+Cmd+End

Select to end of Command Window.

Left arrow key

Ctrl+B

Left arrow key

Move back one character.

Right arrow key

Ctrl+F

Right arrow key

Move forward one character.

Ctrl+ Left arrow key

None

Option+ Left arrow key

Move left one word.

Ctrl+ Right arrow key

None

Option+ Right arrow key

Move right one word.

Home

Ctrl+A

Cmd+ Left arrow key

Move to beginning of current statement. With key bindings for Macintosh platforms, move to beginning of current line.

End

Ctrl+E

Cmd+ Right arrow key

Move to end of current statement. With key bindings for Macintosh platforms, move to end of current line.

Esc

Ctrl+U

Esc

Clear the command line when cursor is at the command line. Otherwise, move cursor to command line.

Delete

Ctrl+D

Forward Delete

Delete character after cursor.

Backspace

Ctrl+H

Delete

Delete character before cursor.

None

Ctrl+K

None

Cut contents (kill) from cursor to end of current line.

Insert

None

None

Change to overwrite mode from insert mode, or change to insert mode from overwrite mode. View current mode in the status bar: OVR is gray for insert mode. In overwrite mode, what you type replaces existing text and the cursor is a wide block. (Not supported on Macintosh platforms.)

Double-click

None

Double-click

Select current word. To select additional words, hold mouse after second click and continue dragging left or right.

NoneNone

Shift+Option+ Left arrow key

Select to previous word.

NoneNone

Shift+Option+ Right arrow key

Select to next word.

Triple-click

None

None

Select current line. To select additional lines, hold mouse after second click and continue dragging up or down.

Shift+Home

None

Shift+Cmd+ Left arrow key

Select from cursor to beginning of statement. With key bindings for Macintosh platforms, select to beginning of line.

Shift+End

None

Shift+Cmd+ Right arrow key

Select from cursor to end of statement. With key bindings for Macintosh platforms, select to end of line.

Enter in selection

None

None

Append selection to statement at command line and execute it.

Ctrl+Enter in hyperlink

None

Ctrl+Enter in hyperlink

Open hyperlink displayed in Command Window. For example, in the hyperlink of an error message, opens the file in the Editor/Debugger at that line number.

Navigating Above the Command Line

To look at or copy information in the Command Window that is above the command line (>> prompt), use the mouse and scroll bar, key combinations such as Ctrl+Home, and search features. By default, the up and down arrow keys recall statements so you cannot use them to move the cursor when it is above the command line.

To use the up and down arrow keys to move the cursor when it is above the command line, select File > Preferences > Command Window, and select the Accessibility preference.

  


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