The Wayback Machine - https://web.archive.org/web/20110629105840/http://www.mathworks.com/help/techdoc/matlab_env/f0-5789.html
Skip to Main Content Skip to Search
Product Documentation

Entering Statements in the Command Window

Case and Space Sensitivity

Briefly, MATLAB code is sensitive to casing, but insensitive to blank spaces. For details, see the following sections:

Upper and Lowercasing for Variables, Files, and Functions

In MATLAB code, use an exact match with regard to case for variables, files, and functions. For example, if you have a variable a, you cannot refer to that variable as A. It is a best practice to use lowercase only when naming functions. This is especially useful when you use both Microsoft Windows and UNIX[1] platforms because their file systems behave differently with regard to case.

Note that if you use the help function, the help displays function names in all uppercase, for example, PLOT, solely to distinguish a function name from the rest of the text. Some functions for interfacing to Sun Microsystems Java software do use mixed case and the command-line help and the documentation accurately reflect that.

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

Cut, Copy, Paste, and Undo Features

To edit text in the Command Window, choose Cut, Copy, Paste, Undo and Redo from the Edit menu.

Undo applies to some of the actions listed in Edit menu. You can perform an undo operation multiple times in succession. Redo reverses an Undo.

Press the Esc (escape) key to clear everything you have entered on the current line.

If you press 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.

Entering Multiple Lines Without Running Them

To enter multiple statements on multiple lines before running any of the statements:

  1. Type a statement on a line, and then use the keyboard shortcut for Break Line Without Code Execution (which is Shift+Enter by default).

    The cursor moves down to the next line, which does not show a prompt.

  2. Type the next statement.

  3. Repeat steps 1 and 2 until you have typed all the statements you want.

  4. Edit the statements, if needed.

Then, to run all of the lines, press Enter or Return.

For example, if you enter the following:

>> a=1 % Press Shift+Enter to advance without executing this statement.
b=2    % Press Shift+Enter to advance without execution. You can edit this or the above line.
c=a+b  % Press Enter to execute all three statements.

MATLAB executes all three lines and returns the following:

a =
     1
b =
     2
c =
     3
>>

When you enter a paired keyword statement on multiple lines, such as for and end, you do not need to use Shift+Enter. You can use the typical process of pressing Enter after each line in the set to advance to the next line. MATLAB executes the keyword statement after you complete it on the last line. For example:

>> for r=1:5  % Press Enter. MATLAB advances a line where you continue the paired keyword statement.
a=pi*r^2      % Press Enter. MATLAB advances a line where you continue the paired keyword statement.
end           % Press Enter to execute the paired keyword statement.

MATLAB executes all three lines and returns the following:

a =
   3.141592653589793
a =
  12.566370614359172
a =
  28.274333882308138

See also Performing Desktop Actions Using Keyboard Shortcuts.

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 suppresses the output for the command preceding the semicolon. For example, type three functions on one line to build a table of logarithms, and then press the Enter or Return key:

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

MATLAB runs the functions in order, from left-to-right.

Entering Multiple-Line (Long) Statements Using Line Continuation

To enter a multiple-line statement:

  1. At the end of the line, indicate that the statement continues on the next line by entering three periods (...), also called dots, stops, or an ellipsis.

    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.

  2. Press the Enter or Return key.

  3. Continue typing the statement on the next line.

  4. Repeat steps 1 through 3 until you complete the statement.

  5. Press Enter or Return when you complete the statement.

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.

Recalling Previous Lines in the Command Window

Assuming you have not changed the default keyboard shortcuts for the arrow, tab, and control keys, you can recall, edit, and reuse functions you typed earlier by using these keys. 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 keys do not behave as documented here, check the actions currently assigned to them, as described in Displaying Keyboard Shortcuts.

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, and so by default, you cannot use these keys 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, customize the keyboard shortcuts for the Cursor Up and Cursor Down actions in the Keyboard Shortcuts preferences. See also Customizing Keyboard Shortcuts.

See Also


[1] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


Recommended Products

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

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