Sunteți pe pagina 1din 16

MECN2012: Computing

Skills and Software


Development
Lecture 9:
MatLab Data Types, Logical &
Boolean Operators, Functions &
Debugging

Data Types
Numerical:
Basic unit in MatLab
Includes both scalars and matrices
Entered using [ ]
e.g. A = [1 2;3 4] produces
1 A2=

34
Denoted in workspace by

Data Types
String:
Contains character data
Typically arises from data input or
output
Can also be used for entering ASCII
characters (chars) using the char
function
Typically entered using
e.g. A = Apple

Denoted in workspace by

Data Types
Cell Array:
Stores strings or other cell arrays in a matrix-like
form
Typically used for GUI input or output functions
Entered using { } and
e.g. A = {string1 string2;string3 string4}
produces

string1
string2
A=
string3
string4
Denoted in workspace by

Data Types
Structure Array:
Contains various data forms in fields
Once specified, a field can only contain
one type of data
Structure arrays also have layers
Recognisable by . separating structure
array name and field name e.g.
Structure(n).field
Denoted in workspace by

Logical Operators
The syntax for the logical operators in MatLab
is (mostly) identical to the written form
2 Modes of operation:
Matrix-Scalar:
Compares each matrix value, A ij, to the scalar value, B11
Returns true (1) or false (0) in the matching output
matrix, Xij

Matrix-Matrix:
Compares each matrix value, A ij, to the corresponding
matrix value, Bij,and returns true (1) or false (0) in the
output matrix, Xij
Matrices MUST have the same dimensions

Logical Operators
e.g. A =1 2 , B = 1 1 , C = 8
34
00
Ope A (Oper) B
r

A (Oper) C

Oper

A (Oper) B

A (Oper) C

=
=
<

10
00

00
00

>

01
11

00
00

00
00

11
11

11
11

00
00

<
=

10
00

11
11

>=
()
~=

01
11

11
11

(NOT
)

Logical Operators
Also specific operators for strings
Can compare strings with == but must
be same length
2 forms of string comparator:
strcmp: compares strings by looking at
length then each letter considering case
strcmpi: compares strings by looking at
length then each letter disregarding case

Logical Operators
Can also be used to compare a cell
array of strings to a single string
e.g. A = Appl, B = apple, C = pear
e
pear
Oper

Oper(A,B)

Oper(A,C)

Oper(B,C)

strcmp

0
0

0
1

strcmpi

1
0

0
1

Functions
Stored in m-files and called by the name
thereof
Specified by an opening declaration, function,
which lists inputs and outputs in order
Inputs separated by commas
Outputs separated by commas and listed in
square brackets (if there is more than one)
Functions do not necessarily need to accept
inputs from the command line or return
outputs to the workspace

Functions
function [Output1, Output2,
,Outputm] = Name(Input1, Input2,
,Inputn)
Requires Inputs 1 to n, in order, be
specified when the function is called
Returns Outputs 1 to m, in order, to the
workspace once the function is
completed
The variable names Inputi and Outputj
are internal to the function (scope is
limited to function) i.e. are not the same

Functions
function Output = Name(Input1, Input2,
,Inputn)
Requires Inputs 1 to n, in order, be specified
when the function is called
Returns a single Output to the workspace

function Output = Name(Input)


Requires only a single Input be specified
when the function is called
Returns a single Output to the workspace
when complete

Functions
An output variable must be defined in the
course of the operation of a function if
one is defined in the declaration
otherwise an error will result
Not all inputs passed to a function need
be used
Can also have functions which use other
means of data input and output which will
declare no variables in the declaration

Debugging
Finding and correcting faults in a program
Can change the inputs and outputs of the
function definition as programming progresses
to monitor particular results
Can leave screen output unsuppressed (no ;) to
monitor variable values in real time
Can use break points in code to pause operation
and interrogate workspace inside of function
MatLab returns error codes indicating line on
which error occurred and nature of variable

Class Example
Bubble sort
Simple algorithm used for sorting a vector of values
into ascending or descending order by directly
comparing each value to the one below it and
swapping the values if necessary
Called a bubble sort since the smallest (or largest)
values bubble to the top with each successive pass
Must pass through the vector as many times as the
number of elements to ensure complete sorting (i.e.
in case the largest value is at the top (in the case of
an ascending bubble sort))

Sta
rt

Is c2 <
A?

No
Vector
(V)
c1 = 1
c2 = 1
A = length(V)

c2 = 1
temp3 = c1
c1 = temp3 +
1
Is c1
A?

Ye
s
No

Ye
s
Is V(c2)
>
V(c2+1
)?
Ye
temp1
s =
V(c2+1)
V(c2+1) =
V(c2)
V(c2) =
temp1

No

temp2 = c2
c2 = temp2 +
1

return V

End

S-ar putea să vă placă și