Introduction to MATLAB
Lecture 1
- MATLAB Environment
o % Sign is to comment – MATLAB does not interpret this sign
o Just having text in the editor window does not do anything
MATLAB only cares about code that is run in the command
window
o How to execute commands from Editor window
Copy/paste the code into the command window to evaluate
Make a selection --> F9
Make a selection --> Right click --> Evaluate selection
- MATLAB as a Simple Calculator
o Spacing improves readability
1*3+6/2
o Parentheses improves readability
8*2/2^2 turns to (8*2)/(2^2)
o Parentheses override the order of operations
(8*2/2)/^2
(3+6)/9
- Colon Operator
o Generates number sequences
1 : 5 (this can be written as 1 : 1 : 5)
This command counts by 1
1:2:5
This command counts by 2
X : Y :Z
Count from X to Z in increments of Y
80 : -1 : 70
This command counts backwards by increments of 1
o MATLAB stays within the boundaries and will never go above the
last number
- Variables
o What are variables?
Variables are necessary in coding
There are different types of variables for storing different
kinds of information
Variables can store numbers, strings, cells, structures
and more
MATLAB workspace acts as a buffer
Variables are placeholders for information in the
MATLAB workspace
Square brackets
Link things together in a chain
Curly brackets
Make cell arrays
o Rules for variable name
It cannot start with a number, but can contain numbers
It cannot use special characters
The underscore is allowed
Variable names are case-sensitive
Keep short but long enough so you know what they mean
Single-letter variable names should be avoided except
in very short loops
Short, meaningful names
Avoid bult-in variables/function names
Select variable names that other people could interpret
o Classes of variables
Double
Numbers
Characters
Letters
Logical
Boolean and take on values of true (1) or false (0)
Cell
Cells are like cages that enclose pieces of information
(could be strings or numbers or even other cells)
They are created using curly brackets
Structure
Structures are arguably the most flexible ways to
store data in MATLAB
A structure contains fields of any class, and are
indicated by a period between variable name and one
of its fields
o Scalar
A single number
Single numbers have only magnitude, not direction
o Variables for numbers
Numbers
5+5
X stands for number
o X=5
o X+5
Any variable name is possible, but MATLAB etiquette
suggests simplicity
o XX = 7 (x, y, z are better)
You can reassign a variable to a different value
o Reassigning variables: Now 7 overwrite variable
x
X = 7.9
Calculations using variables
o Y=6
o Z=8
o Z+Y
Output can be stored in another variable
o a=z*y
o 10 * a - 5.456789 + z– x
Use of semi-colon to suppress the output
o When you type something like:
o b = 45; it will not print it out again
anArray
One-dimensional matrices and can be created using
square brackets
o anArray = [1 2 3 5 3 6 7 9];
aMatrx
Two-dimensional matrices can be created by using
semi-colon to indicate where the next row of
numbers begins
o aMatrx = [1 3 2; 5 3 6; 7 8 9]
o Variables for characters
n = ‘matlab’
MATLAB prints out the character array
Semi-colon to stop output
o Variables for strings
- Commands
o Who
Lists the variables in the command window currently
o Whos
Gives more information about variables
o Clear
Removes variable from workspace
Clear A
o Clear all
Removes all variables from workspace
o Cd
Change directory
o Pwd
Shows current directory
o Is
Lists files in current directory
o Clc
Clear command window
- Rows and Columns
o Row vector/array
A = [1 2 3 4 5 6]
o Column vector/array
A = [1
2
3
4
5
6
]
- Matrices
- Useful build-in functions
o Creating Linear sequences
Linspace (x1, x2, N)
x1, x2 and N are known as the arguments to the function
This function generates N points linearly between x1 and x2
If N is not defined it defaults to 100
>> a = linspace(1, 19, 10)
>> a = 1 3 5 7 9 11 13 15 17 19
o Creating non-linear sequences
Logspace(x1, x2, N)
This function generates N points logarithmically equally
spaced points between 10x1 and 10x2
If N is not defined it defaults to 50
>> a = logspace(1, 2, 5)
>> a = 10.0000, 17.7828, 31.6228, 56.2341, 100.0000
o Creating 2-D Data
For creating large or higher dimensional matrices it is
usually easier to create the matrix first with a function and
then to fill it in as needed
>> a = zeros(N)
o a is now a 2-D matrix of size N x N with all
elements set to 0 (square matrix)
>> a = zeros(M, N)
o a is now a 2-D matrix of size M x N with all
elements set to 0
>> b = ones(N)
o b is now a 2-D matrix of size N x N with all
elements set to 1
>> b = ones(M, N)
o b is now a 2-D matrix of size M x N with all
elements set to 1
o Creating random numbers
>> c = rand(N)
Returns an N-by-N matrix containing pseudorandom
values drawn from the standard uniform distribution
on the open interval (0,1).
>> c = rand(M,N)
Returns an M-by-N matrix containing pseudorandom
values drawn from the standard uniform distribution
on the open interval (0,1).
Randn instead of rand produces the same results as rand,
but random numbers from normal distribution
>> d =randi(IMAX, N)
Returns an N-by-N matrix containing pseudorandom
integer values drawn from the discrete uniform
distribution on 1:IMAX
IMAX means Integer Maximum
>> d = randi(IMAX, M, N)
Returns an M-by-N matrix containing pseudorandom
integer values drawn from the discrete uniform
distribution on 1:IMAX
o Length()
Returns the length for 1D vector
>> a = 1:10
>> length (a)
>> ans = 10
o If the input is a matrix, length() returns the length of the biggest
dimension
>> b = ones(2,5)
>> length(b)
>> ans = 5
o Size()
Report exact size of matrix (number of rows, number of
columns)
>> size(b)
>> ans = 2 5
- Colon = range
- Comma = specific values
Lecture 2 – Scripts and Functions
- Scripts
o MATLAB scripts are the simplest form of the MATLAB file in which
you can write your MATLAB code
- Soft-coding vs hard-coding
o