You are on page 1of 9

Unless otherwise specified, MATLAB stores calculations in a variable named ‘ans’

...

‘=’ is the assignment operator

The expression on the right of = is assigned to the variable on the left.

When entered x=3+4, MATLAB first evaluates 3+4, then assigns the result 7 to variable X

...

Workspace window (on the right) shows all the variables currently in the workspace

...

Adding a semicolon to the end of a command will suppress the output ( though the comman
will still be executed) as you can see in the Workspace.

When entered a command without a semicolon at the end MATLAB displays the result in the
command window.

...

Previous commands can be recalled by pressing Up arrow key on the keyboard

(Command Window must be the active window for this to work

...

When you enter just a variable name at the command prompt, MATLAB returns the current
value of that variable

...

You can name your MATLAB variables anything you’d like as long as they start with a
letter and contain only letters, numbers, and underscores ( _ ).

...

Command ‘ clear’ can clear all variables (look at the WORKSPACE window to see the
change while entering the command clear)

‘clear’ removes WORKSPACE variables, and

...

Command ‘clc’ clears the COMMAND WINDOW


...

2.3 Using Built-in Functions and Constants

pi represents 𝜋

(although four decimal places are shown, it is calculated for more precision internally)

abs calculates absolute value

eig calculate eigenvalues

sin(x) calculates the sin value of the variable x

sqrt calculates the square root

...

MATLAB uses parentheses () to pass inputs to functions, similar to standard mathematical


notation.

e.g:

>> x=pi/2

x=1.5708

y=sin(x)

y=1

...

Imaginary number i is a build in constant in MATLAB


3.1 Manually Entering Arrays

All MATLAB variables are arrays

meaning that each variable can contain multiple elements.

A single number, called a scalar, is actually a 1-by -1 array

meaning it contains 1 row and 1 column

...

You can create arrays with multiple elements using square brackets

e.g.

>> x= [3 5]

x= 3 5

...

When you separate numbers by spaces (or commas) MATLAB combines the numbers into a
row vector (an array with one and multiple columns [(1-by –n), here 1 is the #of rows and n is
the #of columns]).

When you separate them by semicolons, MATLAB creates a column vector (n-by-1)

>> x= [7;9]

x=7

...

You can combine spaces and semicolons to create matrices (arrays with multiple rows and
columns)

When entering matrices, you must enter them row by row.

>> x= [3 4 5; 6 7 8]

x=345

678
...

You can perform calculations within the square brackets

>>x=[abs(-4) 4^2]

x=4 16

...

Alternative, shorthand method for creating evenly spaced vectors

first:last

e.g.

>> y=5:8

y=5 6 7 8

...

‘:’ operator uses a default spacing of 1 between two consecutive numbers.

You can specify your own spacing as shown below

e.g.

>> x=20:2:26

x=20 22 24 26

...

If you know the number of elements you want in a vector (instead of the spacing between
each element), you could instead use the

linspace(first, last, number_of_elements)

It will randomly create a vector with the number of elements you inserted which start and end
at the requested first and last number. Here the space between two consecutive numbers will
be automatically decided.

Note the use of commas (,) to separate inputs to the linspace function

>> x=linspace(0,1, 5)

x= 0 0.250 0.500 0.750 1.000


...

Both linspace and : operator create row vectors.

However, you can convert a row vector into a column vector using the transport operator
(’)

>> x=1:3;

>> x=x’

x=1

...

You can create column vectors in a single command by creating the row vector and
transporting it all on one line.

Note the use of parentheses here to specify the order of operations

>> x=(1:2:5)’

x=1

5
...

3.3 Array Creation Functions

MATLAB contains many functions that help you to create commonly used matrices, such as
matrices of random numbers

Note that the 2 in the command rand(2) specifies that the output will be a 2-by-2 matrix of
random numbers

...

zeros ( , )

x=zeros(6,3) creates a 6-by-3 matrix with all elements containing zeros


4.1 Saving and Loading Variables

You can save variables in your WORKSPACE to a MATLAB specific file format called
MAT-file using save command

>> save foo x

Saves a variable x to a MAT-file named foo.mat

...

You can load variables from a MAT-file using the load command

...
5.0 Import Tool

Data stored in a text file can be imported to MATLAB WORKSPACE by selecting that file
in current folder, right click select import data

A preview of the data file is shown

Data is highlighted in blue

Any missing data in the text file is highlighted in orange and labelled as NaN which stands
for Not a Number

You can chose how to import data, for example as a single metrix

The name of the metrix to be created can be changed if you want

Select import selection button

Create the desired variable in the MATLAB WORKSPACE

...
...

5.1 Indexing into Arrays

You can extract values form an array using row , column indexing

>> x=A(5,7)

Extracts the value in 5th row and 7th column of A and assigns the result to the variable x

...

Use MATLAB keyword end as either a row or column index to reference the last element

>> x=A(end, 2)

NOTE: you can use arithmetic with the keyword end

>> x=A(end -1, end-2)

Value stored in the last row-1st and last column-2nd place of the A matrix/array is extracted

...