lecture 15

37
Lecture 15 Chapters 6 and 7

Upload: hilary-hayes

Post on 03-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Lecture 15. Chapters 6 and 7. Outline from Chapter 6. 6.1 Character String Concepts: Mapping and Casting 6.2 MATLAB Implementation 6.2.1 Slicing and Concatenating Strings 6.2.2 Arithmetic and Logic Operations 6.2.3 Useful Functions 6.3 Format Conversion Functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture  15

Lecture 15

Chapters 6 and 7

Page 2: Lecture  15

Outline from Chapter 66.1 Character String Concepts: Mapping and Casting6.2 MATLAB Implementation

6.2.1 Slicing and Concatenating Strings6.2.2 Arithmetic and Logic Operations6.2.3 Useful Functions

6.3 Format Conversion Functions6.3.1 Conversion from Numbers to Strings6.3.2 Conversion from Strings to Numbers

6.4 Character String Operations6.4.1 Simple Data Output: the disp(…) function

6.4.2 Complex Output6.4.3 Comparing Strings

6.5 Arrays of Strings

Page 3: Lecture  15

Characters and Numbers

• Unicode: provides a unique number for every character,no matter what the platform,no matter what the program,no matter what the language.http://unicode.org/standard/WhatIsUnicode.html

• ASCII: a smaller code for fewer characters, represents characters with binary number code

• It is certainly possible to have characters that are digits. ‘124’

Page 4: Lecture  15

Character Mapping

• A kind of mapping, i.e., “one from column A is related to one from column B”

• Specifically, an element from column A is a character, such as ‘r’ or ‘\tab’ or ‘\bell’ andan element from column B is a number.

• When the context implies that a number is to be interpreted as a character, the mapping is used in reverse to convert the number into its corresponding character.

Page 5: Lecture  15

Casting• The default context comes from the type of a variable, which comes from

the last time it was assigned.• We can force the context by casting.

>> myArray = [97 98 99 100 101]myArray = 97 98 99 100 101>> myString = char(myArray)myString =abcde>> uint8(myString)ans = 97 98 99 100 101

Page 6: Lecture  15

Implicit Casting

>> fred='Fred'

fred =

Fred

>> next = fred+1 <- contents of variable fred interpreted in the context of number, due to the +1

next =

71 115 102 101

Page 7: Lecture  15

Extraction from, Combination of Strings

>> firstName = 'George Washington';>> lastName = 'Carver';>> wholeName = [firstName, ' ',lastName]wholeName =George Washington Carver>> middleName = wholeName(8:17)middleName =Washington

Page 8: Lecture  15

Logic on Character StringsmiddleName>'Z'ans = Columns 1 through 6 0 1 1 1 1 1 Columns 7 through 10 1 1 1 1>> ischar(wholeName)ans = 1>> isspace(wholeName)ans = Columns 1 through 6 0 0 0 0 0 0 Columns 7 through 12 1 0 0 0 0 0 Columns 13 through 18 0 0 0 0 0 1 Columns 19 through 24 0 0 0 0 0 0

Page 9: Lecture  15

Character Strings of Digits

• People using MATLAB intend numerical interpretation, but type character strings of digits.

• There is a more efficient numerical representation used within MATLAB, and the computer in general, than character strings of digits.

• MATLAB performs the conversions between the two.

Page 10: Lecture  15

To String - 1>> y = sqrt(391)

y =

19.7737

>> int2str(y)

ans =

20

>> num2str(y,9)

ans =

19.7737199

>> num2str(y, 15)

ans =

19.7737199332852

Page 11: Lecture  15

To String - 2>> niceString = sprintf('Here are some tabbed columns:\n%f\t%f\t%f\t%f',...123,456,789, 99)niceString =Here are some tabbed columns:123.000000 456.000000 789.000000 99.000000>> niceStringdc = sprintf('Here are some tabbed columns:\n%.2f\t%.2f\t

%.2f\t%.2f',...123,456,789, 99)niceStringdc =

Here are some tabbed columns:123.00 456.00 789.00 99.00

Page 12: Lecture  15

From String to Number—If…• >> goodString = '124'

• goodString =

• 124

• >> nonNumericString = '4r5t6y'

• nonNumericString =

• 4r5t6y

• >> sscanf(goodString,'%d')/2

• ans =

• 62

• >>

>> sscanf(nonNumericString,'%d')/2

ans =

2

Page 13: Lecture  15

Data Output

>>disp(‘the answer is’)the answer is>> fprintf('the answer is %s makes perfect,

maybe %d%% of the time', 'practice', 75)the answer is practice makes perfect, maybe

75% of the time>>

Why is the prompt at the end?

Page 14: Lecture  15

String Output

>> fprintf('the answer is %s makes perfect, maybe %d%% of the time\n', 'practice', 75)

the answer is practice makes perfect, maybe 75% of the time

>>

What’s the difference? The \n is a newline.

Page 15: Lecture  15

String Comparison

>> firstString = 'ATGC'firstString =ATGC>> secondString = 'ATTC'secondString =ATTC>> firstString == secondStringans = 1 1 0 1

>> thirdString = 'ATT'

thirdString =

ATT

>> firstString == thirdString??? Error using ==> eqMatrix dimensions must agree.

>> strcmp(firstString, thirdString)

ans =

0

Page 16: Lecture  15

More String Comparison>> thirdString = 'ATT'thirdString =ATT>> fourthString = 'att'fourthString =att

>> strcmp(thirdString, fourthString)ans = 0

>> strcmpi(thirdString, fourthString)ans = 1

Page 17: Lecture  15

Arrays of Strings

• Remember that arrays have a number of columns• That number of columns applies to every row.• To make strings of all the same length, one way is:>> aStringArray=char('Timoshenko', 'Maxwell', 'Mach')

aStringArray =

TimoshenkoMaxwell Mach

Page 18: Lecture  15

Outline from Chapter 7 -- 1

• 7.1 concept: Collecting Dissimilar Objects• 7.2 Cell Arrays– 7.2.1 Creating Cell Arrays– 7.2.2 Accessing Cell Arrays– 7.2.3 Using Cell Arrays

• 7.3 MATLAB Structures– 7.3.1 Constructing and Accessing One Structure– 7.3.2 Constructor Functions

Page 19: Lecture  15

Outline from Chapter 7 -- 2

• 7.4 Structure Arrays– 7.4.1 Constructing Cell Arrays– 7.4.2 Accessing Structure Elements– 7.4.3 Manipulating Structures

Page 20: Lecture  15

Three Heterogeneous Collections

• Cell arrays : index their contents with a numerical index

• Structures : identify components with symbolic index

• Structure arrays : index member structures with numerical index

Page 21: Lecture  15

Accessing Data Within Heterogeneous Collections

• Access by index (much as we have been doing with arrays), called cell arrays

• Access by name (name of field within structure)

• Can create arrays of structures

Page 22: Lecture  15

Creating Cell Arrays

• Cell arrays can be created by assigning values to an indexed variable.

>> A{1}=42A = [42]>> B{1}={[4 6]}B = {1x1 cell}>> C={3,[1,2,3], 'abcde'}C = [3] [1x3 double] 'abcde'

Page 23: Lecture  15

Accessing Cell Arrays

• (conventional) Arrays of Containers• Compare accessing the array to obtain the

containervsaccessing the content of the container

Page 24: Lecture  15

Example Create Cell Array D

>> D = [A B C {'xyz'}]

D =

Columns 1 through 4

[42] {1x1 cell} [3] [1x3 double]

Columns 5 through 6

'abcde' 'xyz'

Page 25: Lecture  15

Example Access Cell Array

• Access a container in D, familiar notation>> D(1)ans = [42]• Access contents of container in D>> D{1}ans = 42

Page 26: Lecture  15

More Accessing Cell Array>> a={3,[1,2,3] 'abcde'}a = [3] [1x3 double] 'abcde'>> a{1:2}ans = 3ans = 1 2 3>> [x y]=a{1:2}x = 3y = 1 2 3>> b([1 3])=a([1 2])b = [3] [] [1x3 double]

Page 27: Lecture  15

deal Function -- 1

• Access the cell array:• >> help deal• DEAL Deal inputs to outputs.• [A,B,C,...] = DEAL(X,Y,Z,...) simply matches up the

input and• output lists. It is the same as A=X, B=Y, C=Z, ...• [A,B,C,...] = DEAL(X) copies the single input to all• the requested outputs. It is the same as A=X, B=X,

C=X, ...•

Page 28: Lecture  15

Operations on Heterogeneous Collections

• With multiple datatypes making up the heterogeneous collections, collective operations do not really apply

• Instead, first extract the part, then do the appropriate operation, then reassemble

aStringArray =

TimoshenkoMaxwell Mach

>> aStringArray+1

ans =

Columns 1 through 8

85 106 110 112 116 105 102 111 78 98 121 120 102 109 109 33 78 98 100 105 33 33 33 33

Columns 9 through 10

108 112 33 33 33 33

>> char(aStringArray+1)

ans =

UjnptifolpNbyxfmm!!!Nbdi!!!!!!

Page 29: Lecture  15

Processing with Cell Arrays

<initialize result>for <index specification> <extract an element> <check the element accordingly> <process the element accordingly>end<finalize result>

Page 30: Lecture  15

Extract, Check “Accordingly”

• Find the class of an element: class(item) returns a string suitable for use in a switch statement

• isa(item, ‘class’)• iscell(item)• ischar(item)• islogical(item)• isnumeric(item)• isstruct(item)

Page 31: Lecture  15

A Little More Cell Processing

function ans = totalNums(ca)%count the numbers in a cell arrayans = 0;for i = 1:length(ca)

item = ca{i}if isnumeric(item)

ans = ans+prod(size(item));endend

<initialize result>for <index specification> <extract an element> <check the element

accordingly> <process the element

accordingly>end<finalize result>

Page 32: Lecture  15

Structures

• Heterogeneous, like cell arrays• Instead of accessing by sequence number,

access by field name• Have fields,– Name– Datatype• Any MATLAB object

• Can have arrays of structures

Page 33: Lecture  15

Creating One Structure*• The structure is created incrementally—

it emerges:>> myStruct.item1 = 3

myStruct =

item1: 3

>> myStruct.item2 = 'abce'

myStruct =

item1: 3 item2: 'abce'

myStruct.goodFieldName = 'example'

myStruct =

item1: 3 item2: 'abce' goodFieldName: 'example‘

>> myOtherStruct = rmfield(myStruct, 'item2')

myOtherStruct =

item1: 3 goodFieldName: 'example'

*Created as a variable, not as a datatype

Page 34: Lecture  15

Another Way to Create One Structure*

>> struct('studentName', 'Elaine',...'targetDegree', 'PhD',...'nChildren', 1,...'location', 'home',...'age', 65)

ans =

studentName: 'Elaine' targetDegree: 'PhD' nChildren: 1 location: 'home' age: 65

*Created as a variable, not as a datatype

Page 35: Lecture  15

A Way to Create Multiple Instances of One Structure*

function ans = makeCD(gn, ar, ti, yr, st, pr)%populate CD structure with provided CD data

ans.genre = gn;ans.artist = ar;ans.title = ti;ans.year = yr;ans.stars = st;ans.price = pr;

end*Created as a variable, not as a datatype, but the function fills the template as if it were a type.

Page 36: Lecture  15

Function in Action

CD=makeCD('Blues', 'Charles, Ray', 'Genius Loves Company', 2004, 4.5, 15.35)

CD =

genre: 'Blues' artist: 'Charles, Ray' title: 'Genius Loves Company' year: 2004 stars: 4.5000 price: 15.3500

Page 37: Lecture  15

Many Instances of One Type of Structure

• An array of data items, each of which contains the same fields of information.

>> cds(1) = makeCD('Blues', 'Clapton, Eric', ...'Sessions For Robert J', 2004, 2, 18.95)

cds =

genre: 'Blues' artist: 'Clapton, Eric' title: 'Sessions For Robert J' year: 2004 stars: 2 price: 18.9500

>>

>> cds(2)=makeCD('Classical',...'Bocelli, Andrea', 'Andrea', 2004, 4.6, 14.89)

cds =

1x2 struct array with fields: genre artist title year stars price