After reading this lesson, you should be able to:
use the Command Window and understand its function,
control what appears in the Command Window,
access MATLAB documentation through the Command Window.
The Command Window (Figure 1) is the main window used to display the program output in MATLAB. We can also program directly into the Command Window; however, our code will not be saved in an m-file.
Figure 1: The default MATLAB window configuration is shown with m-file open. The Command Window is at the bottom center.
Table 1: Commonly used mathematical operators.
Operation | Syntax |
---|---|
Add | + |
Subtract | - |
Multiply | * |
Divide | / |
Power | ^ |
Square Root | sqrt() |
This means you can quickly test a few lines of code, use it as a
calculator, or check the value of a variable (see Figure 2). This also
means it can be a useful tool when debugging (find errors in the code)
in your program. To use basic mathematical operators and numerals, just
input the statement and hit the enter key. Table 1 shows a few common
mathematical operators commonly used in MATLAB. Note that when the
answer is displayed, it is assigned the name, ans
, by
default.
Figure 2: Command Window used for a quick calculation (left), outputting the value of a previously defined variable (middle), and a quick and small program (right).
It is important to note that MATLAB does not understand what an
equation is as known to you in a traditional sense. In MATLAB there is a
variable name side and an expression side to any statement, and these
sides are divided by an “equal to” sign. The variable name is always on
the left side and the expression is always on the right side of an equal
to sign. How this works is that MATLAB reads a variable name and
attaches that variable name to what is on the right side of the equal to
sign. This variable name is now associated with the number or expression
on the right side of the equal to sign. If you do not assign the
expression to a variable name, then MATLAB automatically assigns a
default name, ans
, to the expression.
The Command Window will display the outputs of all executable code that is in the m-file. In most cases, the programmer may only want the final solution to be displayed, suppressing outputs of all other lines. Suppressing variables can also decrease the run time of your program (if it is long). To make this possible, MATLAB has a special character that can be added to any line of the m-file – the suppression character. This suppression is made possible by inserting a semicolon (;) at the end of any line of code. It is important to note that although the output of a line followed by the semicolon (;) is not displayed in the Command Window, the calculation in the line is still being done “behind the scene” by MATLAB. An example of using this character to suppress outputs from the Command Window is shown in Example 1.
Important
Note: Suppressing a line of code will only change whether
it outputs to the Command Window or not: it does not stop MATLAB from
performing the operation.
Suppress variables from being output to the Command Window.
Solution
If you cannot remember syntax/usage while you are programming, the help
and doc
commands can be used directly in the Command Window to pull up
documentation on a specific MATLAB function or command.
The difference between help
and doc
is that help
contains a summary of the documentation and displays directly in the
Command Window (see Figure 3) while doc
opens a new window with the full documentation page from MathWorks. Both
are quick and easy ways to review documentation.
The Command Window does have limitations. In fact, it is rarely used to perform mathematical operations or for doing MATLAB programming. The main reasons for this are that it is difficult to change expressions without overwriting them and displaying useful information in the Command Window is cumbersome. For the purposes of this book, the main role of the Command Window is to only display the output information from a MATLAB file (also called an m-file). Although it is important to understand the inner workings of the Command Window, the m-file is the basis of MATLAB programming as described in Lesson 1.5.
Figure 3: Using the help
command to retrieve documentation from the Command Window.
Task | Syntax | Example Usage |
Suppress an output (Command still processed) |
; |
code; |
Get summary of documentation for a MATLAB command or function | help |
>>help disp |
Open documentation page for a MATLAB command or function | doc |
>>doc disp |
Add two variables | + |
a+b |
Subtract two variables | - |
a-b |
Multiply two variables | * |
a*b |
Divide two variables | / |
a/b |
Raise a variable to a power | ^ |
a^2 |
Take the square root of a variable | sqrt() |
sqrt(a) |