Lesson 1.6

The Command Window


  • Understand the Command Window’s function within the MATLAB program.
  • Control what is displayed in the Command Window.
  • Learn common, helpful uses for the Command Window.

What Is the Command Window?

The Command Window in MATLAB is where most of your programming outputs are displayed. The Command Window can be easily identified as the MATLAB environment subwindow with the “Command Window” label and a input prompt symbol “>>”. Figure 1 shows our default location of the Command Window as laid out in lesson [LINK TO LESSON].

Figure 1: Command window location highlighted in the Default MATLAB environment layout.

We saw examples of outputs in the “Hello World” lesson. We can also program directly into the Command Window; however, our code will not be saved in an m-file. This means you can quickly test a few lines of code, use it like a calculator, or check the value of a variable (see Figure 2). This also means it can be a useful tool when debugging (find problems) in your program.

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).

Control What Is Displayed (Output) to the Command Window

In many cases, we will want to stop some variables from being displayed in the Command Window. This is commonly called “suppressing” variables. Suppressing variables can decrease the run time of our program (if it is long) and also make it easier for us to see the outputs we are interested in.

To suppress a line of code in MATLAB, place a semicolon (;) at the end of the line. As seen in Example 1, only the variable a shows up in the Command Window because we suppressed the output of variable b with a semicolon at the end of the statement.

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.

Example 1: Suppressing Variables
a = 2 %Creating an unsuppressed variable b = 3; %Creating an suppressed variable (note the semicolon) c = a*b; %There is no Command Window output from this line because we suppressed it. c %Demonstrating that “c” has been set to a*b (suppressing only stops the display output!). %Note, if you are confused by which line is displaying “c = 6” in the Command Window Output below, %you should rerun this code yourself and unsuppress all outputs. It should be immediately clear %what is happening. Command Window Output
a = 2 c = 6 [Try this code yourself with Octave Online! Click Here]

How to View Documentation in the Command Window

If you cannot remember syntax 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 them 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, which you can also see by clicking the links in here in the lesson text. Both are quick and easy ways to review documentation.

Figure 3: Using the help command to retrieve documentation in the Command Window.

Next Lesson

In the next lesson, we will cover how to use the Command Window effectively.