Main Content

Command Window

Select the Command Window


The Command Window enables you to enter individual statements at the command line, indicated by the prompt (>>). As you enter statements, the Command Window displays the results.

For example, to create the variable a, type a = 1 at the command line and press Enter. MATLAB® adds the variable to the workspace and displays the result in the Command Window. To suppress the display of output, end statements with a semicolon, for example, a = 1;.

If you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of your calculation. The value of ans changes with every command that returns an output value that is not assigned to a variable.

This table describes some additional actions that you can perform in the Command Window.

ActionHow to Perform the Action
Enter multiple statements on multiple lines before running any of the statements.

Enter the multiple statements at the command line, pressing Shift+Enter between statements.

This key combination is unnecessary when you enter a paired keyword statement on multiple lines, such as for and end.

Clear a statement from the command line without executing it.

Press the Escape (Esc) key.

Recall previous statements.

Press the Up arrow ↑ key. The Command History window opens and displays a log of previous statements.

To recall a specific statement, type any part of the statement and then press the Up arrow key. For example, to recall the command b = 2, type b, and then press the Up arrow key.

Clear the Command Window.

Call the clc function.

To clear the Command Window without deleting any text, call the home function instead. Calling the home function moves the cursor to the upper-left corner of the Command Window and scrolls all visible text out of view, giving the appearance of clearing the screen without deleting any text.

Evaluate a statement already in the Command Window.

Select a statement, right-click, and then select Evaluate Selection.

Execute only a portion of the code currently at the command line.

Select the code at the command line and press Enter.

Command line showing two disp commands separated by a comma, with the first disp command selected

MATLAB Command Window

Open the Command Window

The Command Window is always open. To bring focus to the Command Window from another tool such as the Editor, or to make the Command Window visible if it is minimized, type commandwindow.

To restore the Command Window to the default location and size, go to the Home tab, and in the Environment section, click Layout. Then, select from one of the preconfigured desktop layout options.


expand all

Create variables and call functions in the Command Window.

Create a variable named a by typing this statement at the command line. MATLAB adds the variable a to the workspace and displays the result in the Command Window.

a = 1
a = 1

Calculate the sine of a by calling the sin function without specifying an output variable. MATLAB uses the variable ans to store the result of the calculation.

ans = 0.8415

Create a variable named b and end the statement with a semicolon. MATLAB performs the computation but suppresses the display of output in the Command Window.

b = a + 2;

Enter multiple statements on the same line. Separate the statements by ending each one with a comma or semicolon. Statements that end with a comma display their results, while statements that end with a semicolon do not.

A = magic(5),  B = ones(5) * 4.7;  C = A./B
A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

C =

    3.6170    5.1064    0.2128    1.7021    3.1915
    4.8936    1.0638    1.4894    2.9787    3.4043
    0.8511    1.2766    2.7660    4.2553    4.6809
    2.1277    2.5532    4.0426    4.4681    0.6383
    2.3404    3.8298    5.3191    0.4255    1.9149

Create a line plot and then bring focus back to the Command Window.

Create a line plot by typing these statements at the command line. MATLAB creates a figure containing the line plot. Focus moves to the figure.

x = 0:pi/100:2*pi;
y = sin(x);

Figure contains an axes object. The axes object contains an object of type line.

Bring focus back to the Command Window.


Programmatic Use

expand all

commandwindow selects the MATLAB Command Window. For example, type commandwindow in your script or live script after a plotting command to bring focus back to the Command Window.

commandwindow is not supported when running MATLAB with the -nodesktop option.


  • To determine the number of columns and rows that appear in the Command Window in its current size, type matlab.desktop.commandwindow.size in the Command Window. MATLAB returns the number of columns and rows, respectively. For example:

    ans =
       133    24

    If the Set matrix display width to eighty columns Command Window preference is selected, the number of columns is 80. For more information, see Set Command Window Preferences.

  • To display text or the value of a variable in the Command Window, use the disp function. For example, this code creates a variable and displays its value in the Command Window.

    A = [15 150];
        15   150
    You also can use the fprintf function to display text.

Version History

Introduced before R2006a