Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 45 ยท Getting Started

The Desktop, Command Window and Scripts

Tour the MATLAB and Octave desktop: command window, editor, workspace, current folder, scripts and live scripts, the ans variable and the semicolon.

Before you write real programs, it helps to know where code goes and where results show up. MATLAB and Octave both give you a command window that works like a calculator with memory, a workspace that remembers your variables, and an editor for saving code in files. Once you understand how these pieces fit together, most beginner confusion ("where did my variable go?", "why is it printing everything?") disappears.

This lesson is a guided tour. The desktop programs cannot run inside a web page, so the examples here are read-only MATLAB/Octave transcripts, plus Python stand-ins that mimic how the workspace and ans behave. For a first look at what the two programs are, see What Are MATLAB and Octave?.

The main parts of the desktop

When you start MATLAB, you see several panels. Octave's graphical interface has nearly the same layout.

The four panels you use daily

Command Window Type a line, press Enter, see the result.
Workspace Every variable that exists right now.
Current Folder Where your files are saved and found.
Editor Write longer programs in files.

Octave calls the same ideas Command Window, Workspace, File Browser and Editor.

1 / 2
  • Command Window shows a prompt (>> in MATLAB, >> in Octave too). You type a command and press Enter; the result appears immediately.
  • Workspace lists the variables you have created, with their size and type. Clicking a variable opens it in a table viewer. Commands such as who and whos print the same list as text.
  • Current Folder is the folder MATLAB and Octave look in for your files. A script or function you saved elsewhere cannot be run by name unless its folder is on the path.
  • Editor is a text editor with syntax colors, line numbers and a Run button. Files you create there end in .m.

Octave without its graphical interface is just the command line: a prompt, your typing and the output. Everything in this lesson applies to it as well.

The command window as a calculator

Type an expression without assigning it, and the result is stored in a special variable called ans:

MATLAB
>> 3 + 4
ans =

     7

>> ans * 2
ans =

    14

ans always holds the result of the last expression that was not assigned to a named variable. That lets you chain quick calculations, but do not rely on it in saved programs, where an explicit name is clearer.

When you do assign, the program prints the new value:

MATLAB
>> radius = 2.5
radius =

    2.5000

>> area = pi * radius^2
area =

   19.6350

The default display shows four digits after the decimal point. That is only how the number is shown; the stored value has full double precision. The next lesson, Variables, Output and Help, shows how to change the display.

The semicolon: quiet mode

A line that does not end with a semicolon prints its result. A line that ends with ; runs silently:

MATLAB
>> x = 10;
>> y = x * 3;
>> y
y =

    30

This matters a lot with big arrays. Creating a 1000-by-1000 matrix without a semicolon would print a million numbers. As a habit, end assignment lines with a semicolon and print only what you want to look at.

Mimicking the workspace in Python

The Python stand-in below acts like a tiny command window. A dictionary plays the role of the workspace, and a helper decides whether to print and whether to update ans. It is not MATLAB, but it makes the rules visible: an unassigned expression updates ans; an assignment with a semicolon is silent.

Python A toy command window (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that radius was created but never printed, while area and both ans results were. The workspace has three entries at the end: ans, area and radius.

Looking inside the workspace

In MATLAB, whos lists each variable with its size, memory use and class. The sizes are always rows by columns. A single number is 1x1, and doubles use 8 bytes each:

MATLAB
>> a = 5; v = [1 2 3]; M = zeros(2, 3);
>> whos
  Name      Size            Bytes  Class     Attributes

  M         2x3                48  double
  a         1x1                 8  double
  v         1x3                24  double

You can reproduce that table in NumPy to check the arithmetic. This stand-in builds the same three variables and prints name, size and bytes.

Python A whos-style listing (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The float64 class in NumPy corresponds to MATLAB's double. The byte counts match because both use 8 bytes per element.

Scripts: saving your work

The command window forgets your commands when you close it. To keep a program, save the commands in a script: a plain text file with the extension .m. For example, a file called circle_area.m might contain:

MATLAB
% circle_area.m - area of a circle
radius = 2.5;
area = pi * radius^2;
fprintf('The area is %.2f\n', area);

To run it, make sure the file's folder is the current folder, then type its name without the extension:

MATLAB
>> circle_area
The area is 19.63

The editor's Run button does the same thing. A script has no inputs or outputs of its own: it works directly in the workspace, so after running it you will see radius and area listed there. That is convenient for experiments but means a script can overwrite variables you already had.

For reusable pieces of code with their own inputs and outputs you write functions, covered in Scripts and Functions.

Live scripts and the Octave editor

MATLAB also offers live scripts (extension .mlx). They mix code, formatted text, results and plots in one document, a bit like a notebook. They are a MATLAB feature and Octave does not open them, so use ordinary .m files if you want code that works in both.

Octave's graphical editor works with plain .m files. If you run Octave from a terminal you can also start a script with octave circle_area.m.

Useful housekeeping commands

A handful of commands keep the session tidy. They are all one-word commands that you can type without parentheses.

MATLAB
>> clc          % clear the Command Window text
>> clear        % delete all variables from the workspace
>> clear x y    % delete only x and y
>> pwd          % print the current folder
>> ls           % list files in the current folder
>> cd newfolder % change the current folder

Two keyboard habits save a lot of time: the up arrow brings back earlier commands, and typing the first letters of a command followed by the up arrow finds the last one that started that way. Tab completes names of variables and functions.

If a program runs too long or seems stuck, press Ctrl+C in the Command Window to interrupt it.

Tip: Start every script with clear; clc; while you are learning. It deletes leftover variables so you know your result comes from the code in front of you, not from something typed earlier.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You type 5 * 4 without assigning it. Where is the result stored?

    An expression that is not assigned to a named variable updates ans, which always holds the most recent unassigned result.
  2. 2. What does a semicolon at the end of a line do?

    The command still runs and the variable is still created, but nothing is printed. This keeps large results from flooding the Command Window.
  3. 3. What is the main difference between a script and typing commands in the Command Window?

    A script is a saved list of commands. Running it executes them in order in the current workspace.
  4. 4. Which command deletes all variables from the workspace?

    clear removes variables. clc only clears the text in the Command Window, who lists variables and pwd prints the current folder.

Summary

  • The Command Window runs one command at a time; the Workspace lists your variables; the Current Folder decides where files are found; the Editor holds longer programs.
  • An unassigned expression stores its result in ans.
  • A trailing semicolon suppresses printing but not execution.
  • who and whos show the workspace; clear empties it and clc clears the screen.
  • Scripts are .m files run by name and share your workspace. Live scripts (.mlx) are MATLAB-only.

Next, learn to name things and print results in Variables, Output and Help.