You are on page 1of 33

Basic MATLAB concepts

Georg Fries
Georg Fries Modelling and Simulation using MATLAB
2
Georg Fries Modelling and Simulation using MATLAB
3
What is MATLAB?
MATLAB is an interactive system for numerical computation, programming
and visualization.
MATLAB can be used for a wide range of applications, i.e. digital signal
processing, image processing, robotics, control engineering, financial
computing or computational biology.
The built-in tools and math functions enable you to solve complex numerical
problems in a fraction of time required by using traditional programming
languages such as Java or C++ .
With its relatively simple programming concept, MATLAB can be easily
extended to create scripts and new functions.
You can import and export your data for analyzing and visualizing.
Georg Fries Modelling and Simulation using MATLAB
4
Command
Window
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
Georg Fries Modelling and Simulation using MATLAB
6
Georg Fries Modelling and Simulation using MATLAB
7
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
8
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
Georg Fries Modelling and Simulation using MATLAB
10
Fundamental MATLAB Data Types
Name Descr|pnon Use
doub|e, s|ng|e

lloaung-olnL numbers use for fracuonal numerlc daLa
uouble and slngle preclslon
uefaulL numerlc Lype ln MA1LA8
|nt8, |nt16,
|nt32, |nt64
lnLegers use for slgned whole numbers (lnLegers)
4 slzes: 8, 16,32 and 64 blLs
u|nt8, u|nt16,
u|nt32, u|nt64
unslgned lnLegers use for unslgned whole numbers (lnLegers)
4 slzes: 8, 16,32 and 64 blLs
char CharacLers and SLrlngs uaLa Lype for LexL, for muluple sLrlngs, use cell arrays
|og|ca| Loglcal Cperauons use ln relauonal condluons or Lo LesL sLaLe
1wo values: Lrue or false
funcnon hand|e luncuon Pandles olnLer Lo a funcuon
tab|e 1ables ConLalner for mlxed-Lype, column-orlenLed daLa
struct SLrucLures llelds sLore arrays of varylng classes and slzes
ce|| Cell Arrays Cells sLore arrays of varylng classes and slzes
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
Georg Fries Modelling and Simulation using MATLAB
12
Array and Matrix Arithmetic Operators
Cperator Name Comments
+ (plus) array and maLrlx addluon C = A+8 : adds A and 8
- (mlnus) array and maLrlx subLracuon C = A-8 : subLracLs 8 from A
.* (umes) array element-wise multiplication C = A.*8 : ls Lhe elemenL-by-elemenL producL of A and 8
* (mumes) maLrlx mulupllcauon C = A*8 : ls Lhe llnear algebralc producL of maLrlces A and 8
.] (rdlvlde) array rlghL dlvlslon A./8 : ls Lhe maLrlx wlLh elemenLs A(l,[)/8(l,[)
] (mrdlvlde) maLrlx rlghL dlvlslon x = 8/A : ls Lhe soluuon of Lhe equauon xA = 8
.\ (ldlvlde) array le dlvlslon A.\8 : ls Lhe maLrlx wlLh elemenLs 8(l,[)/A(l,[)
\ (mldlvlde) maLrlx le dlvlslon x = A\8 : ls Lhe soluuon of Lhe equauon Ax = 8
.^ (power) array elemenL-wlse power A.^8 : ls Lhe maLrlx wlLh elemenLs A(l,[) Lo Lhe 8(l,[) power
^ (mpower) maLrlx power A^8 : ls A Lo Lhe power 8, lf 8 ls a scalar
.' (Lranspose) array Lranspose A.' : ls Lhe array Lranspose of A.
' (complex
conj. transpose)
maLrlx Lranspose A' : ls Lhe llnear algebralc Lranspose of A
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
13
Relational Operators
Name Descr|pnon
== (eq)
A == 8 reLurns a loglcal array wlLh elemenLs seL Lo loglcal 1 (Lrue) where arrays A and 8 are equal
CLherwlse, lL reLurns loglcal 0 (false)
Compares boLh real and lmaglnary parLs of numerlc arrays
~= (ne)

A ~= 8 reLurns a loglcal array wlLh elemenLs seL Lo loglcal 1 (Lrue) where arrays A and 8 are noL equal
CLherwlse, lL reLurns loglcal 0 (false)
Compares boLh real and lmaglnary parLs of numerlc arrays
> (gt)
A > 8 reLurns a loglcal array wlLh elemenLs seL Lo loglcal 1 (Lrue) where A ls greaLer Lhan 8
CLherwlse, lL reLurns loglcal 0 (false) , Compares only Lhe real parL of numerlc arrays
< (|t)
A < 8 reLurns an array wlLh elemenLs seL Lo loglcal 1 (Lrue) where A ls less Lhan 8
CLherwlse, lL reLurns loglcal 0 (false) , Compares only Lhe real parL of numerlc arrays
>= (ge)
A >= 8 reLurns a loglcal array wlLh elemenLs seL Lo loglcal 1 (Lrue) where A ls greaLer Lhan or equal Lo 8
CLherwlse, lL reLurns loglcal 0 (false) , Compares only Lhe real parL of numerlc arrays
<= (|e)
A <= 8 reLurns a loglcal array wlLh elemenLs seL Lo loglcal 1 (Lrue) where A ls less Lhan or equal Lo 8
CLherwlse, lL reLurns loglcal 0 (false) , Compares only Lhe real parL of numerlc arrays
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
14
Logical Operators
Name Descr|pnon
& (and)
A & 8 performs a loglcal Anu of Lhe lnpuL arrays A and 8 and reLurns an array conLalnlng
elemenLs seL Lo elLher loglcal 1 (Lrue) or loglcal 0 (false)
An elemenL of Lhe ouLpuL array ls seL Lo 1 lf all lnpuL arrays conLaln a nonzero elemenL aL LhaL
same array locauon, oLherwlse, LhaL elemenL ls seL Lo 0
~ (noL)
~A performs a loglcal nC1 of lnpuL array A, and reLurns an array conLalnlng elemenLs seL Lo elLher
loglcal 1 (Lrue) or loglcal 0 (false)
An elemenL of Lhe ouLpuL array ls seL Lo 1 lf Lhe lnpuL array conLalns a zero value elemenL aL LhaL
same array locauon, oLherwlse, LhaL elemenL ls seL Lo 0
| (or)
A | 8 performs a loglcal C8 of Lhe lnpuL arrays A and 8 and reLurns an array conLalnlng elemenLs
seL Lo elLher loglcal 1 (Lrue) or loglcal 0 (false)
An elemenL of Lhe ouLpuL array ls seL Lo 1 lf any lnpuL arrays conLaln a nonzero elemenL aL LhaL
same array locauon, oLherwlse, LhaL elemenL ls seL Lo 0
xor
C = xor(A, 8) performs an excluslve C8 operauon on Lhe correspondlng elemenLs of arrays A and 8
1he resulung elemenL C(l,[,...) ls loglcal Lrue (1) lf A(l,[,...) or 8(l,[,...), buL noL boLh, ls nonzero
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
15
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
16
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
17
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
18
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
20
Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB
Georg Fries Modelling and Simulation using MATLAB
22
Control Flow
Name Descr|pnon
|f - elself - else LxecuLe sLaLemenLs based on a specled loglcal condluon
for LxecuLe sLaLemenLs for a xed number of umes
sw|tch - case - oLherwlse SwlLch among several cases based on expresslon
wh||e 8epeaL execuuon of sLaLemenLs whlle condluon ls Lrue
try - caLch LxecuLe sLaLemenLs and caLch resulung errors
break 1ermlnaLe execuuon of a for or a whlle loop
connnue ass conLrol Lo Lhe nexL lLerauon of a for or a whlle loop
end 1ermlnaLe block of code
pause PalL execuuon Lemporarlly
return 8eLurn Lo Lhe lnvoklng funcuon
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
23
Control Flow
Selection statements, that enable us to make choices as to whether
statements are executed or not during run time
if statement
switch statement
Loop control statements, that allows us to repeatedly execute a block of
code

for statement
while statement
Georg Fries Modelling and Simulation using MATLAB
24
if statement
if condition
action
end


if condition_1
action_1
elseif condition_2
action_2
!
else
action_n
end
The if statement evaluates the condition, and
executes the following action if the condition
is true

elseif and else are optional
they execute actions only when previous
conditions are false!
An if block can include multiple elseif
statements
The if statement requires the end keyword
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
25
if statement example
clc
% generate a random integer number between 1 and 1000
a = randi(1000)
% check the size
% include alternate choices, using elseif or else.
if a > 500
disp('The number is large, it is greater than 500')
elseif a > 100
disp('The number is greater than 100 and smaller than 501')
else
disp('The number is small')
end
Georg Fries Modelling and Simulation using MATLAB
26
if statement example
clc
% generate a random integer number between 1 and 1000
a = randi(1000)
% check the size
% include alternate choices, using elseif or else.
if a > 500
disp('The number is large, it is greater than 500')
elseif a > 100
disp('The number is greater than 100 and smaller than 501')
else
disp('The number is small')
end
Georg Fries Modelling and Simulation using MATLAB
27
switch statement
switch switch_expression
case case_expression1
action_1
case case_expression2
action_2
...
otherwise
action_n
end
A switch block conditionally executes one set of
actions from several choices
Each choice is a case
An evaluated switch_expression is a scalar or string
An evaluated case_expression is a scalar, a string,
or a cell array of scalars or strings
The switch block tests each case until one of the
cases is true
When a case is true, MATLAB executes the
corresponding actions, and then exits the switch
block!
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
28
switch statement example
% Check if it is Weekend
% Get the day of the week in the variable dayString
[dayNum, dayString] = weekday(date, 'long', 'en_US');
switch dayString
case 'Saturday'
disp(dayString)
disp('Hi - tomorrow is Sunday!')
case 'Sunday'
disp(dayString)
disp('Hi - it is Sunday, enjoy your weekend')
otherwise
disp(dayString)
disp('Hi - sorry you have to work today!')
end
Georg Fries Modelling and Simulation using MATLAB
29
for loop
for index = values
program statements
:
end

A for loop executes one or more statements in a loop
values has one of the following forms:
initval:endval
increments the index variable from initval to endval by 1 and
repeats execution of program statements until index is greater
than endval
initval:step:endval
increments index by the value step on each iteration, or
decrements when step is negative
valArray
creates a column vector index from subsequent columns of
array valArray on each iteration
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
30
for loop example
% plotting an animated cosine
% Using drawnow in a loop causes the display
% to update while the loop executes:
clc
x = 0:pi/100:2*pi;
y = cos(x);
h = plot(x,y,'YDataSource','y');
% use a step of 0.01 on each iteration
for k = 1:0.01:5
y = cos(x.*k);
refreshdata(h,'caller')
drawnow
end
Georg Fries Modelling and Simulation using MATLAB
31
while loop
while expression
action
end
A while loop repeatedly executes an action in a
loop as long as an expression remains true
An evaluated expression is true when the result
is nonempty and contains all nonzero elements
(logical or real numeric). Otherwise, the
expression is false
Expressions can include
relational operators (such as < or ==) and
logical operators (such as &&, ||, or ~)
MATLAB evaluates compound expressions from
left to right, adhering to operator precedence rules
8eference: hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
32
while loop example
clc
% Find the first integer n for which factorial(n) is a 51-digit number
n = 1;
nFactorial = 1;
while nFactorial < 1e51
n = n + 1;
nFactorial = nFactorial * n;
end
% echo n and nFactorial
disp('The first integer n for which factorial(n) is a 51-digit number is:')
n
nFactorial
adopLed and modled from hup://www.maLhworks.com/help/maLlab
Georg Fries Modelling and Simulation using MATLAB
33
while loop example
clc
% Find the first integer n for which factorial(n) is a 51-digit number
n = 1;
nFactorial = 1;
while nFactorial < 1e51
n = n + 1;
nFactorial = nFactorial * n;
end
% echo n and nFactorial
disp('The first integer n for which factorial(n) is a 51-digit number is:')
n
nFactorial

You might also like