16
MECN2012: Computing Skills and Software Development Lecture 9: MatLab – Data Types, Logical & Boolean Operators, Functions & Debugging

Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

  • Upload
    ali-gh

  • View
    213

  • Download
    1

Embed Size (px)

DESCRIPTION

hg

Citation preview

Page 1: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

MECN2012: Computing Skills and Software Development

Lecture 9:MatLab – Data Types, Logical & Boolean

Operators, Functions & Debugging

Page 2: 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 A =

– Denoted in workspace by

1 23 4

Page 3: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 4: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

A =

– Denoted in workspace by

string1 string2string3 string4

Page 5: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 6: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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, Aij, to the scalar value, B11

• Returns true (1) or false (0) in the matching output matrix, Xij

– Matrix-Matrix: • Compares each matrix value, Aij, to the corresponding matrix

value, Bij,and returns true (1) or false (0) in the output matrix, Xij

• Matrices MUST have the same dimensions

Page 7: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

Logical Operators

• e.g. A = , B = , C = 81 23 4

1 10 0

Oper A (Oper) B A (Oper) C Oper A (Oper) B A (Oper) C

== >

< >=(≥)

<=(≤)

~= (NOT)

1 00 0

0 00 0

1 00 0

0 00 0

1 11 1

1 11 1

0 11 1

1 11 1

0 11 1

0 00 0

0 00 0

1 11 1

Page 8: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 9: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

Logical Operators

• Can also be used to compare a cell array of strings to a single string

• e.g. A = , B = ‘apple’, C = ‘pear’

Oper Oper(A,B) Oper(A,C) Oper(B,C)

strcmp 0

strcmpi 0

‘Apple’‘pear’

00

10

01

01

Page 10: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 11: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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 as in the calling environment

Page 12: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 13: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 14: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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

Page 15: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

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))

Page 16: Lecture+9+-+MatLab+-+Data+Types,+Logical+_+Boolean+Operators,+Functions+_+Debugging

A = length(V) c2 = 1

return V

c1 = 1c2 = 1

temp3 = c1c1 = temp3 + 1

temp2 = c2c2 = temp2 + 1

temp1 = V(c2+1)V(c2+1) = V(c2)V(c2) = temp1

Start

End

Is V(c2) > V(c2+1)?

Is c2 < A?

Is c1 ≤ A?

No

Yes

Vector (V)

NoYes

No Yes