MATLAB environment

These windows are in your MATLAB environment:

All windows can be found under HOME -> ENVIRONMENT -> Layout.

Commands and Comments

MATLAB can be used as a claculator. A semicolon (;) supresses the output. Use % to write comments.

%Everythng after a % will be ignored.

9 / 2


9 / 2;

%cf. graphing calculator computations

2 + 3 * 10 ^ 4 - 1

(((((2 + 3))))) * 10 ^ (4 - 1)


Use the equals sign to assign a numerical value to a variable name. The variable name must always be on the left. Values of variables can be changed at any time (hence their name 'variable').

x = 3

3 = x  % <- this will lead to an ERROR

%Observe the changes to x in the workspace window:

x^2 + 3*x

x = 5

x^2 + 3*x

%Variables can also be used to define other variables, or even to redefine themselves:

y = x / 3
x = 8*x + 5

%What would happen in the following?

x = 12
y = 3*x - 9 * 2
z = 4*x + 3*y^2 - 10

%x = ?, y = ?, z = ?

Predifend variables in MATLAB

Some variables are prediefined, e.g. pi, inf, i, NaN, ans.

%These can be redefined, but be careful when doing so:

pi = 3; % NO error
pi      % <- this can be dangerous!

Variable names

if = 5 % <- this will lead to an ERROR
%Note: the blue color indicates that 'if' is sth special!

Built-in MATLAB functions

Commonly used functions for math purposes are added for convenience.


sqrt(81)    % square root

abs(-53.2)  % absolute value

log(1000)   % natural log

log10(1000) % log base 10

log2(1000)  % log base 2

% What if I want to compute log with base 3?

%Trigonometric functions (radians and degrees):

sin(pi / 2)

sind(90)    % <- this should be the same as sin(pi / 2)!!!

cos(pi / 2)

%CAUTION: using a function as variable name can be dangerous:
sin = 10    % that works (NO error)
sin(pi / 2) % <- observe the different ERROR messages
sin(3)      % <- observe the different ERROR messages
sin(1)      % <- why does this work?
which sin   % what is going on with sin?

% How do I get back my sin() function?
clear sin
which sin   % check again

% try again:
sin(pi / 2), sin(1) % NOTE: a comma separates multiple commands

Help and Documantetion

Use the MATLAB help to get help! MATLAB has an excellent documentation that is searchable! Access it via the questionmark symbol in the upper right corner of your workspace or type in doc in the command window. Functions can also be looked up in the MATLAB help section for more information.

help sin

doc sin % opens the matlab help for this function (easier to read!)

Managing your workspace

You can save variables into .mat files and load them again.

save my_variables x y pi    % filename and then list the variables
clear                       % deletes all your variables
clc                         % cleans your command window
load my_variables

MATLAB scripts

Scripts are used to create and save code in .m files. A script contains a list of matlab statements (commands, function calls, comments); e.g. VariablesAndTypes.m or example_script.m Evaluate all commands in a script by pressing the EDITOR -> Run button or by typing the following in the Command Window:


run example_script.m

Data Types

The DEFAULT data type in MATLAB is double (double-precision floating-point value). Other data types are logical (boolean) and char (character). A char is defined by quotes, e.g. 'c'

List of numeric data types:

x = 5           % x is a double
y = logical(5)  % y is a logical
c = 'a'         % c is a char

% Find out the type/class of a variable
class(c),  class(x), class(y)
isinteger(c), isfloat(c), isfloat(x)

% Get an overview of all variables (and their types)

Lecture notes for CSE200 (Fall 2015) at Washington University in St. Louis by Marion Neumann (based on materials from Doug Shook).