Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.

Similar presentations


Presentation on theme: "1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1."— Presentation transcript:

1 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

2 1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' 2

3 1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' ans = 1 1 1 1 3

4 1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' ans = 1 1 1 1 Matlab compares and evaluates the equality-condition between each letter 1 by 1, to a 0 (for false) or a 1 (for true) 4

5 1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Frog’ %(note that it is not, so we expect a false!) >> str == 'Frog' ans = 1 1 0 0 5

6 1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Frog’ %(note that it is not, so we expect a false!) >> str == 'Frog' ans = 1 1 0 0 2 letters were identical, 2 letters were not. But.. there is no overall true or false. This is not the way to compare strings. 6

7 1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, is str equal to ‘Flinstones’? %(note that it is not, so we expect a false!) >> str == 'Flintstone’ ??? Error using ==> eq Matrix dimensions must agree. It even gets worse when the length of each string does not match.. It creates an error. Definitely not the right method to compare strings.. 7

8 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ 8

9 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ 9

10 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmp(str, ‘no’) evaluates to _____ 10

11 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmp(str, ‘no’) evaluates to _____ strcmp(‘no’, str) evaluates to _____ 11

12 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ 12

13 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ 13

14 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmpi(str, ‘Yes’) evaluates to _____ 14

15 1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmpi(str, ‘Yes’) evaluates to _____ strcmpi(‘YeS’, str) evaluates to _____ 15

16 Example: Access Granted % ask for username username = input(‘Enter username: ‘, ‘s’); if %correct username % ask for a passwd if %correct password grant access… else quit/end code end else % quit/end code end 16

17 Example: Access Granted % ask for username username = input(‘Enter username: ‘, ‘s’); if strcmpi(username, ‘John’) % correct username %ask passwd pass = input(‘Enter password: ’, ‘s’); if strcmp(pass, ‘u23!9s2’) %if correct password %grant access... else %quit/end code... end else % quit/end code... end 17 The user name may not be case-sensitive… A password is case-sensitive.

18 2. Converting strings Convert: string  numbers str2num() str2double() CAUTION: str2double() will convert an entire cell arrays of strings; str2num() will not. Convert: number  string int2str() num2str() 18 How is this used?

19 Example: prompting inputs Task: Introduce the sensor’s number when prompting. 19

20 Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command: %loop to prompt for each value for position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a space end 20

21 Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command: %loop to prompt for each value for position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a space end CAUTION: they cannot be combined. The input() command accepts only 1 string argument, or 2 when prompting for a string (‘s’).  input() never accepts placeholders and variable names. 21

22 Example: prompting inputs Can be done using string manipulations as well. %loop to prompt for each value for position = 1:nbSensors table(position) = input(['Enter value of sensor #', int2str(position), ': ']); end CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string. 22

23 Example: prompting inputs Can be done using string manipulations as well. %loop to prompt for each value for position = 1:nbSensors table(position) = input(['Enter value of sensor #', int2str(position), ': ']); end CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string. 23 Convert the integer to a string before concatenating all 3 pieces. The digit 1 becomes the string ‘1’, the digit 2 becomes the string ‘2’, etc.. Why:_________

24 Example: validate input Some students have wondered how to handle this issue: 24 “What happens when the user enters letters (instead of numbers)?” Note: as long as variables d and we do not exist, Matlab loops for us. But “what if”…

25 Example: validate input, cont. To solve this problem, every input must now be considered as a string, even if they are numbers! Algorithms possible % Grab user’s input as strings % Use str2double() to convert to numbers % Use isnan() to check if the conversion worked % Continue with calculations if it did 25

26 Example: validate input, cont. To solve this problem, every input must now be considered as a string, even if they are numbers! Algorithms possible % Grab user’s input as strings % Use str2double() to convert to numbers % Use isnan() to check if the conversion worked % Continue with calculations if it did % Grab user’s input as strings % Use str2double() to convert to numbers % while isnan() is true % grab again, convert again % Continue with calculations if it did 26

27 Example: validate input, cont. What does str2double() and isnan() do? Example when string does look like a number: 27

28 Example: validate input, cont. What does str2double() and isnan() do? Example when string has an ERROR: 28

29 Example: validate input, cont. Now, prompt the user for a value, then put the str2double() and isnan() in a loop! 29 EXTREMELY IMPORTANT: isnan() MUST BE THE FIRST CONDITION.

30 3. Additional String Functions strfind() – find a substring within a string lower() – converts a string to lowercase upper() – converts a string to uppercase isletter() – which characters in the string are letters? 30

31 Wrapping Up There are many built-in functions for strings 31

32 Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive 32

33 Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together. 33

34 Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together. str2num(), str2double() converts string  numbers num2str(), int2str() converts numbers  strings 34


Download ppt "1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1."

Similar presentations


Ads by Google