TExcelTAD
Home › MATLAB / Octave › MATLAB Get Started

MATLAB Get Started

To write MATLAB code on your own computer you need either MATLAB (from MathWorks, or MATLAB Online in a browser with a MathWorks account) or GNU Octave (free, from octave.org). Both give you the same three things: a command window, an editor for script files, and a workspace that holds your variables.

Installing

Option How
MATLAB Online Sign in at matlab.mathworks.com (free tier with limited hours)
MATLAB Desktop Download the installer from your MathWorks account or your university licence
GNU Octave (Windows/macOS) Download the installer from octave.org
GNU Octave (Linux) sudo apt install octave (Debian/Ubuntu), sudo dnf install octave (Fedora)

Start the program and you'll see a window with several panels.

The desktop

  • Command Window: type a statement after the >> prompt and press Enter; the result appears immediately.
  • Workspace: lists every variable that currently exists, with its size and class.
  • Current Folder (Octave: File Browser): the folder MATLAB looks in for your scripts and functions.
  • Editor: where you write and save .m files.
  • Command History: the statements you typed earlier (press the up arrow to recall them).

A short session in the Command Window looks like this:

text
>> 7 * 6
ans =
    42
>> radius = 2;
>> area = pi * radius^2
area =
   12.5664

When you don't assign a result to a variable, MATLAB stores it in a special variable called ans. A semicolon at the end of a statement suppresses the display; the statement still runs.

Your first script

A script is a plain text file with the .m extension that contains a sequence of statements. Create a new script in the editor, save it as hello_matlab.m, and type:

matlab · try it yourself
% hello_matlab.m - my first script
disp('Hello, MATLAB!')
name = 'Ada';
fprintf('Nice to meet you, %s.\n', name);
Hello, MATLAB!
Nice to meet you, Ada.

Run it with the Run button (or F5), or by typing its name without .m in the Command Window:

text
>> hello_matlab
Hello, MATLAB!
Nice to meet you, Ada.

Script file names must start with a letter and contain only letters, digits and underscores, so hello_matlab.m works but hello-matlab.m or 1st.m do not.

Comments

Everything after % on a line is a comment. A block of lines can be commented out with %{ and %}, each on its own line:

matlab · try it yourself
% A comment on its own line
speed = 30;        % km/h, a comment after code
%{
  This whole block is ignored.
  Useful for notes or for disabling code temporarily.
%}
time = 2.5;        % hours
disp(speed * time)
75

Tip: In the MATLAB and Octave editors, %% at the start of a line begins a code section. You can run one section at a time with Ctrl+Enter, which is handy for long scripts.

Statements and line continuation

You can put several statements on one line by separating them with commas or semicolons, and continue a long statement onto the next line with three dots ...:

matlab · try it yourself
a = 1, b = 2; c = 3
total = a + b + c + ...
        10 + 20;
disp(total)
a = 1
c = 3
36

Notice that a and c are displayed (comma or no terminator) while b is not (semicolon).

Useful commands while you work

Command What it does
clc Clears the Command Window text
clear Deletes all variables from the workspace (clear x deletes just x)
who / whos Lists variables (whos also shows size and class)
close all Closes all figure windows
Ctrl+C Stops a running program
pwd, cd, ls Show/change the current folder, list files

Many scripts begin with clear; clc; close all; so they start from a clean slate.

Test yourself

What does a semicolon at the end of a statement do?

  1. Ends the program
  2. Suppresses the display of the result
  3. Starts a comment
  4. Makes the variable constant

Where is an unassigned result like 7 * 6 stored?

  1. In result
  2. In ans
  3. Nowhere
  4. In out

Which of these is a valid script file name?

  1. my-script.m
  2. 2nd_try.m
  3. data_clean.m
  4. data clean.m