ee 2300 matlab tutorial - utdallas.edu

56
1 Matlab Basic programs. fortan or C with interact to ability : GUI program n Applicatio 5. computing. advanced to elementary from ranging function in - built Library Function al Mathematic Matlab The 4. ion visualizat D - 3 & D - 2 commands & function : Graphics Handle 3. workspace manage to Facility : t Environmen Working 2. language g programmin level High : language Matlab The 1. : parts 5 has system Matlab GUI. including nt developeme n applicatio & analysis , simulation & modelling n, computatio for used Today oratory. Lab rix Mat for ds tan s Matlab

Upload: others

Post on 18-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EE 2300 Matlab Tutorial - utdallas.edu

1

Matlab Basic

programs. fortanor C with interact to ability

:GUI program nApplicatio 5.

computing. advanced

to elementary from ranging function in-built

Library Function alMathematic Matlab The4.

ionvisualizat D-3 & D-2 commands & function

:Graphics Handle 3.

workspace manage to Facility

:tEnvironmen Working 2.

language gprogrammin level High

:language Matlab The1.

:parts 5 has system Matlab

GUI. including ntdevelopeme napplicatio & analysis

,simulation & modelling n,computatiofor used Today

oratory.Lab rixMatfor dstans Matlab

Page 2: EE 2300 Matlab Tutorial - utdallas.edu

2

MATLAB Basic

3

s

2 1 s

instance,For key. Returnor Enter the press next and command a

type nscomputatio simple a perform Tokey. Returnor Enter

the press next and clc type Window Command theclear To

window. command in programs short simple write can We

(propmt) by followed shown be will logo the pause short

aer window.Aft sterm the of one in Matlab launch UNIXFor

prompt. command is shown.This be will prompt A

appear. will window Command icon, Matlab on clicking double By

:windowsFor

=

+=>>

>>

>>

Page 3: EE 2300 Matlab Tutorial - utdallas.edu

3

MATLAB Basic (cont.)

matfun help

function matrix on examplefor topic, a on help specificFor

....

.transformsFourier and analysis Data - datafun\matlab

algebra.linear numerical - functions Matrix - matfun\matlab

functions. math dSpecialize - specfun\matlab

functions. math Elementary - elfun\matlab

on.manipulati matrix and matrices Elementary - elmat\matlab

.constructs language gProgrammin - lang\matlab

.characters special and Operators - ops\matlab

commands. purpose General - general\matlab

help

:topics HELP

topics. of list a genrate will prompt.It command at help Type

:help line Command

>>

>>

Page 4: EE 2300 Matlab Tutorial - utdallas.edu

4

MATLAB Basic (cont.)

matrices matlab demo

matrices matlabfor examplefor option, demo specific aFor

demo.for window new a open will

demo

demo. type feature, MATLAB on iondemonstratFor

:Demo

... ... ...

division. %Matrix :slash.m

multiply. Matrix *% :mtimes.m

divide. matrix rightor Slash %/ :mrdivide.m

power. Matrix %^ :mpower.m

divide. matrix leftor Backslash \% :mldivide.m

matrixlookfor

keyword thefor files all.M searches lookfor

:lookfor

>>

>>

>>

Page 5: EE 2300 Matlab Tutorial - utdallas.edu

5

MATLAB Basic (cont.)

5)(3/4)(2

.expression the writingfor practice good is () Using

5.7500

= ans

5*3/4+2

right) to left (working nSubtractio & Addition 4.

right) to left (working Division & tionMultiplica 3.

power 2.

brackets 1.

:expression matlab in operaions oforder the

:calculator a as Matlab

++>>

>>

Page 6: EE 2300 Matlab Tutorial - utdallas.edu

6

MATLAB Basic (cont.)

3.0000i + 2.0000

= ans

i*3+2

j.or i byeither denoted is 1- unit imaginary Theform.

r rectangula in drepresente are MATLAB in numbers Complex

numbers complex 3.

point) decimal with (Numbers numbers real 2.

point. decimal the without enterd are Integers

integers 1.

:MATLAB in used numbers of kinds three are There

:Numbers

>>

Page 7: EE 2300 Matlab Tutorial - utdallas.edu

7

Matlab Basic (cont.)

. - or 0.0/0.0 as such operations undefined

allymathematic the of result a as obtained is andnumber -a-not

thefor stands NaN Thezero. by dividing of operation the byor

overflow by generated is Infinity ly.respective infinity, positive

and negative thefor tionsrepresenta the are Inf and Inf– The

NaN 3.

Inf 2.

Inf-1.

:nonnumbers the ngrepresenti variables three has

MATLAB above, mentioned numbers of classes to addition In

∞∞

Page 8: EE 2300 Matlab Tutorial - utdallas.edu

8

Matlab Basic (cont.)

ned.predetermi are valuestheir as

variables as etc, naninf,eps,pi, use tDon' :names Special

z'.' variableor function Undefined ???

5+z=y

18

= y

5+x=y

13;=x

statement. assignment an of side

right on used is it before value a assigned be must variable Each

name. variable a of

characters 31 first the only recognizes MATLAB s.underscoreor

numbers letters, by followed letter, a with begins name Variable

user. the by chosen

are names whose variables in saved are nscomputatio of Results

:Variables

>>

Page 9: EE 2300 Matlab Tutorial - utdallas.edu

9

Matlab Basic (cont.)

Save. onclick next and

.m)(or atfilename.m e.g. file,your for name a Chose

menu. File the from

as Workspace Save select workspace currentyour save To

session.

current in used variables all of list a creates who command

prompt. command from variable acheck also can You

variable. the tab.Choose workspace onClick

Workspace. the

in saved are session MATLAB current the in used variables All

:work reloading & Saving

…→

Page 10: EE 2300 Matlab Tutorial - utdallas.edu

10

Matlab Basic (cont.)

on grid

grid dotted a add to

,....)data2'',data1'legend('

legend put to

)axis'-yylabel('),axis'-xxlabel('

axes label to

) Title'Figuretitle('

title a put to

,...)b.''y2,x,,r-''y1,plot(x,

,multiplotsfor

axis.-x the along isvector first :Note

)r-''y,plot(x,

codes. matlab from figure creates plot command

:Plotting

>>

>>

>>

>>

>>

>>

Page 11: EE 2300 Matlab Tutorial - utdallas.edu

11

Matlab Basic (cont.)

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1sine & cosine wave

x-axis

y-a

xis

sine

cosine

on; grid

,0);cosine'',sine'legend('

);axis'-yylabel('),axis'-xxlabel('

);wave' cosine & sinetitle('

);ro''z,x,,b*''y,plot(x,

x);*cos(pi=z

x);*sin(pi=y

2;:.01:0=x

:Example

>>

Page 12: EE 2300 Matlab Tutorial - utdallas.edu

12

Matlab Basic (cont.)

on; grid

,0);cosine'',sine'legend('

);axis'-yylabel('),axis'-xxlabel('

);wave' cosine & sinetitle('

off; hold

);ro''z,plot(x,

on; hold

);b*''y,plot(x,

x);*cos(pi=z

x);*sin(pi=y

2;:.01:0=x

shown. be will plot last only on hold Without

Here figure. same on plots multiple plot to used be also can hold

>>

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1sine & cosine wave

x-axis

y-a

xis

sine

cosine

Page 13: EE 2300 Matlab Tutorial - utdallas.edu

13

Matlab Basic (cont.)

on; grid

,0);cosine'legend('

);axis'-yylabel('),axis'-xxlabel('

);wave' cosinetitle('

);ro''z,plot(x,

2,3);subplot(2,

on; grid

,0);sine'legend('

);axis'-yylabel('),axis'-xxlabel('

);wave' cosinetitle('

);b*''y,plot(x,

2,2);subplot(2,

x);*cos(pi=z

x);*sin(pi=y

2;:.01:0=x

graph. moreor one plot can we which into

windows,smaller of array nm in splitted is window Graphical

: Subplot

>>

×

0 0.5 1 1.5 2-1

-0.5

0

0.5

1cosine wave

x-axis

y-a

xis

0 0.5 1 1.5 2-1

-0.5

0

0.5

1cosine wave

x-axis

y-a

xis

sine

cosine

Page 14: EE 2300 Matlab Tutorial - utdallas.edu

14

Matlab Basic (cont.)

required. where paste and nsapplicatioother or word to Go

figure. copy choose menu, edit figure From

:nsapplicatioother & word to Copy

menu. Edit figure from modified be

also can properties Axis & properties FigureHowever

Page 15: EE 2300 Matlab Tutorial - utdallas.edu

15

Vectors & Matrices

3

2

1

b

[1;2;3] b

(matrix))vector in row of end marks (Semicolon :vector 2.Column

3 2 1

a

3] 2 [1 a

element) (matrix)vector diff.mark gapor (Comma :vector Row.1

:vectors column & row Creating

=

=>>

=

=

Page 16: EE 2300 Matlab Tutorial - utdallas.edu

16

Vectors & Matrices (Cont.)

same. is operation .' & ' both )matrix( vector real For

2.0000i + 2.0000

1.0000i + 1.0000

= ans

c.'

2.0000i - 2.0000

1.0000i - 1.0000

= ans

c'

i];*2+ 2 i+[1=c

(matrix).vector of transpose the createoperator quote) (dot .' The

3 2 1

= ans

b'

3

2

1

= ans

a'

(matrix).vector of transpose conjugate the createoperator (quote) ' The

:(rows) columns into (columns) rows Transfrom•

>>>>

>>

>>

>>

Page 17: EE 2300 Matlab Tutorial - utdallas.edu

17

Vectors & Matrices (Cont.)

9 4 1

= ans

a.^2

9 4 1

= ans

a*a.

opeartor. dot the follows thatopeartor the of

napplicatio isecomponentwfor used is operator) (dot . The

3

= ans

length(a)

vector. of components of no. the esminerdet:length

>>

>>

>>

Page 18: EE 2300 Matlab Tutorial - utdallas.edu

18

Vectors & Matrices (Cont.)

5 8- 1

= ans

b)cross(a,

[-2,1,2];=b

[1;2;3];=a

3. be mustvector gthlength.Len same of be must B &A

B. and A vectors product cross the returns:cross

6

= ans

b)dot(a,

[-2,1,2];=b

[1;2;3];=a

length. same of be must B &A

B. and A vectors of productscalar the returns:dot

>>

>>

Page 19: EE 2300 Matlab Tutorial - utdallas.edu

19

Vectors & Matrices (Cont.)

value numerical indicares[]

address indicates()

8 7

2 1

= b

)2] [1,3] [1a(=b

:2&1 columns and

3 &1 rows of consisting b submatrix a extract to examplefor

:Submatrix Extracting

9 8 7

6 5 4

3 2 1

= a

9] 8 6;7 5 3;4 2 [1=a

:Matrix Creating

columnsrows

>>

>>

321321

Page 20: EE 2300 Matlab Tutorial - utdallas.edu

20

Vectors & Matrices (Cont.)

value indicates j][i,

position indicates j)(i,

9 7

6 4

3 1

= a

[]=a(:,2) >>

9]; 8 6;7 5 3;4 2 [1=a

[]operator vector empty use :(column) row a delete To

3] 2 [1order in (columns) rows all indicates :

9 7 8

6 4 5

3 1 2

= d

3]) 1 [2a(:,=d

3 2 1

6 5 4

9 8 7

= c

1],:) 2 a([3=c

:ns)rows(colum einterchang To

>>>>

Page 21: EE 2300 Matlab Tutorial - utdallas.edu

21

Vectors & Matrices (Cont.)

5 7 3

= ans

7):2:a(2

3 4 5 6 7 5 3 1

= a

3]:-1:8,6:2:[1=a

order.regular a in bits extarctor build to used be (colon)can :

8

= ans

a(3,2)

10]; 8 6;7 5 3;4 2 [1=a

.position/s specific a of bit/s extarct to used race)is(bracket/b ()

:(matrix)vector of bits gExatractin

>>

>>

>>

Page 22: EE 2300 Matlab Tutorial - utdallas.edu

22

Vectors & Matrices (Cont.)

1 4 9

9 4 1

= ans

a*a.

1]; 2 3;3 2 [1=a

matices.for works alsooperator .(dot) the

9 7

8 5

6 4

3 1

= a

a(3,:)]8]; a(2,:);[5[a(1,:);=a

9]; 6;7 3;4 [1=a

9 8 7

6 5 4

3 2 1

= a

a(:,2)] 8]' 5 [2 [a(:,1)=a

9]; 6;7 3;4 [1=a

:(column) row new a Inserting

>>

>>

>>

Page 23: EE 2300 Matlab Tutorial - utdallas.edu

23

Vectors & Matrices (Cont.)

1 2 4

4- 2- 3

8 1 0

= c

b] [a=c

[8;-4;1];=b

2]; 2;4- 1;3 [0=a

matrices.smaller from from build be can matrix Large

:ionConcatenat Matrix

columns ofnumber

indicates second while rows, of

number indicatesinteger first

matrix. 3-by -3 a its

3 3

= ans

size(a) >>

10]; 8 6;7 5 3;4 2 [1=a

matrix) the of (dimension size the get size

:Matrix a of Size

>>

Page 24: EE 2300 Matlab Tutorial - utdallas.edu

24

Vectors & Matrices (Cont.)

1 0 0

0 1 0

0 0 1

= I

eye(3)=I

matrix identity mm an gives eye(m)

0 0 0

0 0 0

0 0 0

= b

zeros(3,3)=b

s0' of matrix nm gives n)zeros(m,

1 1 1

1 1 1

= a

ones(2,3)=a

s1' of matrix nm an gives n)ones(m,

:Matrices Special

>>

×

>>

×

×

Page 25: EE 2300 Matlab Tutorial - utdallas.edu

25

Vectors & Matrices (Cont.)

37 21

47 23

= ans

b*a

9]; 3 8; 4;6 [2=b

1]; 2 3;3 2 [1=a

B in rows of no. to equal be must A of columns of no.

agree. must dimensions matrixInner

* ==> usingError ???

b*a

8]; 4;6 [2=b

1]; 2 3;3 2 [1=a

:tionMultiplica Matrix

>>

>>

Page 26: EE 2300 Matlab Tutorial - utdallas.edu

26

Vectors & Matrices (Cont.)

9

5

1

= b

diag(a)=b

9]; 8 7 6; 5 4 3; 2 [1=a

matrix. that ofvector diagonal main creates diag matrix a given

:matrix a of diagonal Main

3 0 0

0 2 0

0 0 1

= b

diag(a)=b

3]; 2 [1=a

diagonal. main asvector that with matrix a creates diagvector a given

:matrix Diagonal

>>

>>

Page 27: EE 2300 Matlab Tutorial - utdallas.edu

27

Vectors & Matrices (Cont.)

12

11

10

2

= i

4)==find(a=i

4]; 7 6 4;5 6 5 4;4 3 2 [1=a

condition. given a satisfying matrixor vector

a of (indices) positions the of list a returns find function

:Matrices & Vectorsfor Find

>>

Page 28: EE 2300 Matlab Tutorial - utdallas.edu

28

Solving System of Linear Equations

0.0491

0.5849

= x

b\a=x

ones(3,1);=b

2]; 10;1 1;1- [2=a

)solution Square Least(

system edminerdetOver

nm :2 case

0.0000

1.0000

1.0000-

= x

b\a=x

ones(3,1);=b

10]; 8 6;7 5 3;4 2 [1=a

solution) (Exact

nm :1 case

vector. column ldimensiona m is b & nm size of is A

where b,ax symtemlinear solve to have weConsider

division. left as

known .Also opearator) backslash (\ the :solve to way one

>>

>

>>

=

×

=

Page 29: EE 2300 Matlab Tutorial - utdallas.edu

29

Solving System of Linear Equations (Cont.)

inv(b)*ac means a/bc

b*inv(a)c means b\ac

matrixfor :Note

0.5000

0

0.5000-

= x

b\a=x

ones(2,1);=b

6]; 5 3;4 2 [1=a

)consistent is system the if solutionr (particula

system minedunderdeter

nm:3 case

==

==

>>

<

Page 30: EE 2300 Matlab Tutorial - utdallas.edu

30

Solving System of Linear Equations (Cont.)

column pivotted zero-non of indices the holds case, second in array c

3

= ans

length(c)

3 2 1

= c

0 1 0 0

1 0 1 0

1- 0 0 1

= x

b]) rref([a=c] [x

ones(3,1);=b

10]; 8 6;7 5 3;4 2 [1=a

0

1

1-

= x

x(:,4)=x

0 1 0 0

1 0 1 0

1- 0 0 1

= x

b]) rref([a=x

ones(3,1);=b

10]; 8 6;7 5 3;4 2 [1=a

b.ax system solve to

want we previous, As etc. ncalculatiorank matrix system,linear of solution as such

nsapplicatio manyfor used be can:)form echelon row reduced( rref Function

>>>>

=

Page 31: EE 2300 Matlab Tutorial - utdallas.edu

31

Inverse of a Matrix

matrix. a of inverse find to used be also can rref Function

1.0000 2.0000- 1.0000

2.0000- 3.6667 0.6667-

1.0000 1.3333- 0.6667-

= ans

inv(a)

10]; 8 6;7 5 3;4 2 [1=a

matrix given a of inverse the calculates :inv

>>

Page 32: EE 2300 Matlab Tutorial - utdallas.edu

32

Inverse of a Matrix (Cont.)

1.0000 2.0000- 1.0000

2.0000- 3.6667 0.6667-

1.0000 1.3333- 0.6667-

= b

6):b(:,4=b

1.0000 2.0000- 1.0000 1.0000 0 0

2.0000- 3.6667 0.6667- 0 1.0000 0

1.0000 1.3333- 0.6667- 0 0 1.0000

= b

))])eye(size(a rref([a=b

10]; 8 6;7 5 3;4 2 [1=a

))]);eye(size(a rref([a=b

10]; 8 6;7 5 3;4 2 [1=a

:rref function using matrix a of Inverse⇒

Page 33: EE 2300 Matlab Tutorial - utdallas.edu

33

Determinants

3-

= ans

det(a)

10]; 8 6;7 5 3;4 2 [1=a

ts.determinan gcalculatinfor designed is det Function

>>

Page 34: EE 2300 Matlab Tutorial - utdallas.edu

34

Eigenvalues & Eigenvectors

{ {

seigenvalue are These

0.2599

1.3510-

17.0911

= ans

diag(d)

0.2599 0 0

0 1.3510- 0

0 0 17.0911

= d

0.3158 0.4557 0.8252-

0.8128- 0.1245 0.4978-

0.4895 0.8814- 0.2670-

= v

eig(a)=]d,v[ >>

10]; 8 7 6; 5 4 4; 2 [1=a

matrix. given a of rseigenvecto &

eigenvalus computingfor designed is eig function

vector eigenan is

column each

values eigen as elements

diagonalwith matrix

vectorseigen

of matrix

>>

43421

Page 35: EE 2300 Matlab Tutorial - utdallas.edu

35

Programming in Matlab

arguments. output return &

arguments take may files function Thearguments. output

the returnor arguments input take not do files Script

files Function 2.

files Script 1.

files. .m of type two in contained are codes Matlab

file-M Select

New Choose

window command from menu file to Go

file .m new a open To

files (mat) .m The

Page 36: EE 2300 Matlab Tutorial - utdallas.edu

36

Programming in Matlab (Cont.)

grid

y);plot(x,

sin(x)./x;=y

pi;*10:pi/100:pi/100=x

:file script of Example

>>

0 5 10 15 20 25 30 35-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 37: EE 2300 Matlab Tutorial - utdallas.edu

37

Programming in Matlab (Cont.)

size same of not are matrix

dotprod ==> usingError ???

b)dotprod(a,

5]; [4=b

3]; 2 [1=a

18 10 4

= ans

b)dotprod(a,

[4;5;6];=b

3]; 2 [1=a

function, the access To

end

);size' same of not are matrixerror('

else

q;*p.dp

,length(q))(length(p) if

q)dotprod(p, dp function

:file Function of Example

>>>>

=

==

=>>

Page 38: EE 2300 Matlab Tutorial - utdallas.edu

38

Programming in Matlab (Cont.)

{

body Functuion

end

);size' same of not are matrixerror('

else

q;*p.dp

,length(q))(length(p) if

ndeclaratio Function q)(p,dotprod dp function

variable. return of value return the create to

arguments input the on works funtrion how Deines

:body Function 2.

alue.variable/v ) (returnor

ouput & arguments input name, function icludes

ndeclaratio Function 1.

:parts two has Function

:function a create to How

umentsarginput

namefunction

iablevaroutputkeyword

=

==

=

3214342143421

Page 39: EE 2300 Matlab Tutorial - utdallas.edu

39

Programming in Matlab (Cont.)

dotprod.m

as saved be must file example previous in : examplefor

function. the of name the be must file

the of name the file, M . into function a saving When

folder. same the in be must files

function required the all and files function the accessing

files program the file, .M separate a is file function each

where files function multiple of needs program a If

n.descriptio

bodytheir their before declared be must Function

line. ndeclaratio of end the at coloncomma/semi No

:onconstructi function about something Note

Page 40: EE 2300 Matlab Tutorial - utdallas.edu

40

Programming in Matlab (Cont.)

10

= ans

f(6,8) »

y^2)+sqrt(x^2 = y)f(x,

:function Inline

= f

)y'',x'',y^2)'sqrt(x^2'( inline=f

functions. inline use to handy is it

session, Matlab currentfor only function a use to have we If

:Functions Inline

quote between

arguments

quote between expression function

keyword321

444 8444 76

321 +>>

Page 41: EE 2300 Matlab Tutorial - utdallas.edu

41

Programming in Matlab (cont.)

5050

= sum

sum »

end

i;+sum=sum

100,:1:0=ifor

0;=sum

:Example

end

commands

arraykfor

Structure

:loopsfor 1.

statementsbreak 5.

onsconstructi case-switch 4.

onsconstructi end-else-if 3.

loops while 2.

loopsfor 1.

.statements control 5 have we matlab, In

:Flow Control

>>

=

Page 42: EE 2300 Matlab Tutorial - utdallas.edu

42

Programming in Matlab (cont.)

s(k)sin(k)'*co=a

10;:1:1=k

:ionvectorizat By

end

end

cos(j);*sin(i)=j)a(i,

10:1:1=jfor

10:1:1=ifor

loops for nested By

).jcos()isin(a where matrix 1010 a create we

cases. gprogrammin the

of most in loopsfor of instead used be can which ionvectorizat

called feature a has Matlab ).efficiency time of (because

apply. not do methodsother when used be should loopFor

nested. be also can loopsFor

ij

>>

>>

=×→

Page 43: EE 2300 Matlab Tutorial - utdallas.edu

43

Programming in Matlab (cont.)

0.0061

= q

q »

end

;q/2=q

0.01>q while »

pi;=q

end

statements

expression while

Structure

.beforehand

known not is iteration ofnumber when used is loop This

:loops While 2.

statements

expression

43421

48476

Page 44: EE 2300 Matlab Tutorial - utdallas.edu

44

Programming in Matlab (cont.)

end;

);failed'disp('

else

1; = pass

60)) >= rage(grade_ave & 0.90) >= ce((attendan if

end

else

statements

expression if else

statements

expression if

end

statements

else

statements

expression if

end

statements

expression if

:Structure

:onsconstructi endelseIf .3

end-else-elseif-if

end-else- if

end-if

>>

−−

M

44 844 76

44 844 76

44 844 76

K

Page 45: EE 2300 Matlab Tutorial - utdallas.edu

45

Programming in Matlab (cont.)

}

{

end

);Failed'disp('

otherwise

);C' Gradedisp('

case(2)

);B' Gradedisp('

case(3)

);A' Gradedisp('

(4)case

gpa switch

rand);*ceil(4=gpa

:Example

end

statements

otherwise

statements

2 value case

statements

value1 case

expression switch

:Structure

.statements associate the exucutes it found is match the Once

value. case each to expression input the compares Switch

:onconstructi caseSwitch.4

value

expression

>>

M

Page 46: EE 2300 Matlab Tutorial - utdallas.edu

46

Programming in Matlab (cont.)

required.

not are statementsbreak So, execute. not do statements

caseother the true, is statement case first the If through.

fall not does switch Matlab statement, C Unlike :Note

disp(s)

end

line);strvcat(s, = s

end

break,

ne),isempty(li if

;fgetl(fid) = line

feof(fid)~ while

;'' = s

);r'',fft.m'fopen(' = fid

:example

loop. innnermost the from only exitbreak loops, nested of

case In loop. whileor for from exit early lets statement break

:staements break.5

>>

Page 47: EE 2300 Matlab Tutorial - utdallas.edu

47

Programming in Matlab (cont.)

4 4 4

= ans

a(ind) >>

0 1 1 0 1 0 0

= ind

4)==(a=ind

1]; 4 4 3 4 1 [0=a

false'.' if 0 and true'' are

they when 1 returns and variables

two compares operators These

to) equal (Not ~

to) (Equal

to) equalor (Greater

(Greater)

to) equalor than (Less

than) Less (

operators following the with performed are MATLAB in sComparison

:Operator Logical & Relations

>>

=

==

>=

>

<=

<

Page 48: EE 2300 Matlab Tutorial - utdallas.edu

48

Programming in Matlab (cont.)

isempty[]. :like

functions. logical in-built several are there Matlab In

2.5000 2.0000 1.0000

= ans

a(ind) >>

1 0 0 0 1 1 0 0

= ind

3)<(a&1)>=(a=ind

2.5]; 3 4 3.5 2 1 0.5 [0=a

(not) ~

(and) &

(or) |

MATLAB. in available operators logical three are There

:operator Logical

>>

Page 49: EE 2300 Matlab Tutorial - utdallas.edu

49

Programming in Matlab (cont.)

48 48

24 through 23 Columns

51 50 45 69 69 32 102 111 32 115 115

22 through 12 Columns

97 108 99 32 101 109 111 99 108 101 87

11 through 1 Columns

= ans

ouble(str)d

2300-EE of class Welcome

=str

2300'-EE of class Welcome='str

:string of Example

value. ASCII the from a''letter returns char(s)

a'.'letter for value ASCII the gives )a'double('s

:example for

value. ASCII its by internally

drepresente ischaracter Each .characters of array an is Strings

:Strings

>>

>>

>>

=>>

Page 50: EE 2300 Matlab Tutorial - utdallas.edu

50

Programming in Matlab (cont.)

Matlab. learning are 2300.We-EE of class Welcome

= ans

str2),strcat(str

;Matlab.' learning are We='tr2s

;2300.'-EE of class Welcome='str

strings) two te(concatena strcat

1

= ans

str1),strcmp(str

str;=str1

;2300'-EE of class Welcome='str

true) if 1 return and strings two compare( strcmp

:following are function string important Two

functions. string many are There:Functions String

>>

>>

Page 51: EE 2300 Matlab Tutorial - utdallas.edu

51

Programming in Matlab (cont.)

r);isspace(st

otherwise. 0or whitespace a ischaracter if 1 returns :isspace 2.

0 0 0

25 through 23 Columns

0 0 0 1 1 0 1 1 0 1 1

22 through 12 Columns

1 1 1 0 1 1 1 1 1 1 1

11 through 1 Columns

= ans

tr)isletter(s >>

;2300.'-EE of class Welcome='str

otherwise. 0

or number) (notletter a ischaracter if 1 returns :isletter 1.

.characters categorizefor functions two has Matlab

:characters Categorize

>>

Page 52: EE 2300 Matlab Tutorial - utdallas.edu

52

Programming in Matlab (cont.)

indexing content

indexing cell

arrays. cell accessingfor feature two use we

:arrays Cell 1.

Structure 2.

arrays Cell 1.

structures data

importantother two are there strings & matrix thanOther

:structure dataother

Page 53: EE 2300 Matlab Tutorial - utdallas.edu

53

Programming in Matlab (Cont.)

{

object. array cell-non

a from reference contents Cell ???

c{2}(1,2) >>

7 6 5 3 2 1

= c

b][a,=c

7]; 6 [5=b

3]; 2 [1=a

6

= ans

(1,2){2}c

double] [1x3 double] [1x3

= c

b}{a,=c

7]; 6 [5=b

3]; 2 [1=a

entity.

separate as remain still data nested cell In n.combinatio

linear for Required braces. curly by inputted is Cell :cells

indexingcontent

indexingcell

>>

>>

321

Page 54: EE 2300 Matlab Tutorial - utdallas.edu

54

Programming in Matlab (Cont.)

format matrix

21 24

= ans

[S.age]

or

21

= ans

24

= ans

S.age

field age view to, example For

);,21age'',,456id'',xyz'',name''struct(=S(2)

onconstructi of way one

24;=S(1).age

;='123'S(1).id

;abc'='S(1).name

:structure ngconstructi

s.designator field txtual by accessed elements

with arrays MATLAB sionalmultidimen are Structures .2

wayanother

field 3rdfield 2ndfield 1st

>>

>>

>>

444444444 3444444444 21

484764847644 844 76

Page 55: EE 2300 Matlab Tutorial - utdallas.edu

55

Programming in Matlab (Cont.)

0 1 0

0 1- 2-

1 0 0

= ans

round(r)

0 1 0

0 1- 1-

1 0 0

= ans

fix(r) >>

1 2 1

0 1- 1-

2 1 0

= ans

ceil(r) >>

0 1 0

1- 2- 2-

1 0 1-

= ans

floor(r)

0.3273 1.1909 0.1253

0.0376- 1.1465- 1.6656-

1.1892 0.2877 0.4326-

=r

randn(3)=r

integer) nearest towards dround(roun 4.

zero) towards (round fix 3.

infinity) plus towards (round ceil 2.

infinity) minus towards (roundfloor 1.

:functioninteger to Rounding

>>>>

>>

Page 56: EE 2300 Matlab Tutorial - utdallas.edu

56

Tutorial helpline

s.pdfMatlabNote

0/2300/7Enxa02800llas.edu/%//www.utda:3.http

rials.htmlatlab/tuto.siu.edu/m//www.math:2.http

inc. Mathworks The-Matlab with Started Getting

Computing Technicalof language The-MATLAB 1.