Enter Statements in Command Window
As you work in MATLAB®, you can enter individual statements in the Command Window. For example,
create a variable named
a by typing this statement at the command
a = 1
MATLAB immediately adds variable
a to the workspace and
displays the result in the Command Window.
a = 1
When you do not specify an output variable, MATLAB uses the variable
ans, short for
answer, to store the results of your calculation.
ans = 0.8415
The value of
ans changes with every command that returns an output
value that is not assigned to a variable.
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the Command Window.
b = 2;
To enter multiple statements on multiple lines before running any of the statements,
use Shift+Enter between statements. This action is unnecessary when you
enter a paired keyword statement on multiple lines, such as
You also can enter more than one statement on the same line by separating statements. To distinguish between commands, end each one with a comma or semicolon. Commands that end with a comma display their results, while commands that end with a semicolon do not. For example, enter the following three statements at the command line:
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
MATLAB displays only the values of
in the Command Window.
To recall previous lines in the Command Window, press the up- and down-arrow keys, ↑
and ↓. Press the arrow keys either at an empty command line or after you type the first
few characters of a command. For example, to recall the command
b, and then press the up-arrow key.
To clear a command from the Command Window without executing it, press the Escape (Esc) key.
You can evaluate any statement already in the Command Window. Select the statement, right-click, and then select Evaluate Selection.
In the Command Window, you also can execute only a portion of the code currently at the command prompt. To evaluate a portion of the entered code, select the code, and then press Enter.
For example, select a portion of the following code: