1 / 37

Lecture 15

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

Download Presentation

Lecture 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 15 Chapters 6 and 7

  2. 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 6.3.1 Conversion from Numbers to Strings 6.3.2 Conversion from Strings to Numbers 6.4 Character String Operations 6.4.1 Simple Data Output: the disp(…) function 6.4.2 Complex Output 6.4.3 Comparing Strings 6.5 Arrays of Strings

  3. 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’

  4. 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.

  5. 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

  6. 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

  7. Extraction from, Combination of Strings >> firstName = 'George Washington'; >> lastName = 'Carver'; >> wholeName = [firstName, ' ',lastName] wholeName= George Washington Carver >> middleName = wholeName(8:17) middleName= Washington

  8. Logic on Character Strings middleName>'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

  9. 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.

  10. 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

  11. 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

  12. 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

  13. 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?

  14. 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.

  15. String Comparison >> firstString = 'ATGC' firstString= ATGC >> secondString = 'ATTC' secondString= ATTC >> firstString == secondString ans= 1 1 0 1 >> thirdString = 'ATT' thirdString = ATT >> firstString == thirdString ??? Error using ==> eq Matrix dimensions must agree. >> strcmp(firstString, thirdString) ans = 0

  16. More String Comparison >> thirdString = 'ATT' thirdString= ATT >> fourthString = 'att' fourthString= att >> strcmp(thirdString, fourthString) ans= 0 >> strcmpi(thirdString, fourthString) ans= 1

  17. 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 = Timoshenko Maxwell Mach

  18. 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

  19. 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

  20. 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

  21. 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

  22. Creating Cell Arrays • Cell arrays can be created by assigning values to an indexed variable. >> A{1}=42 A = [42] >> B{1}={[4 6]} B = {1x1 cell} >> C={3,[1,2,3], 'abcde'} C = [3] [1x3 double] 'abcde'

  23. Accessing Cell Arrays • (conventional) Arrays of Containers • Compare accessing the array to obtain the containervsaccessing the content of the container

  24. 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'

  25. 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

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

  27. 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, ...

  28. 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 = Timoshenko Maxwell 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 = Ujnptifolp Nbyxfmm!!! Nbdi!!!!!!

  29. Processing with Cell Arrays <initialize result> for <index specification> <extract an element> <check the element accordingly> <process the element accordingly> end <finalize result>

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

  31. A Little More Cell Processing function ans = totalNums(ca) %count the numbers in a cell array ans = 0; for i = 1:length(ca) item = ca{i} if isnumeric(item) ans = ans+prod(size(item)); end end <initialize result> for <index specification> <extract an element> <check the element accordingly> <process the element accordingly> end <finalize result>

  32. 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

  33. 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

  34. 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

  35. 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.

  36. 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

  37. 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

More Related