Upload
marsha-hines
View
248
Download
0
Embed Size (px)
DESCRIPTION
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 =
Citation preview
1. Comparing Strings2. Converting strings/numbers3. Additional String Functions
StringsBuilt-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' <enter>
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' <enter>ans = 1 1 1 1
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' <enter>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)
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' <enter>ans = 1 1 0 0
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' <enter>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.
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’ <enter>??? Error using ==> eqMatrix 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..
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 ______
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 ______
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 _____
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 _____
12
1. Comparing Strings, cont. Two built-in functions are commonly used to compare
strings:
2. strcmpi() – STRing CoMPare Insensitivereturns true if the two arguments are the same string WITHOUT
REGARD TO CASE
Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______
13
1. Comparing Strings, cont. Two built-in functions are commonly used to compare
strings:
2. strcmpi() – STRing CoMPare Insensitivereturns 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 ______
14
1. Comparing Strings, cont. Two built-in functions are commonly used to compare
strings:
2. strcmpi() – STRing CoMPare Insensitivereturns 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 _____
15
1. Comparing Strings, cont. Two built-in functions are commonly used to compare
strings:
2. strcmpi() – STRing CoMPare Insensitivereturns 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 _____
Example: Access Granted% ask for usernameusername = input(‘Enter username: ‘, ‘s’);
if %correct username% ask for a passwd
if %correct passwordgrant access…
elsequit/end code
endelse % quit/end codeend
16
Example: Access Granted% ask for usernameusername = 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 numbersstr2num()str2double()CAUTION: str2double() will convert an entire cell arrays of strings; str2num() will not.
Convert: number stringint2str()num2str()
How is this used?
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 valuefor position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a spaceend
21
Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command:
%loop to prompt for each valuefor position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a spaceend
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.
22
Example: prompting inputs Can be done using string manipulations as well.
%loop to prompt for each valuefor 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
Example: prompting inputs Can be done using string manipulations as well.
%loop to prompt for each valuefor 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.
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:_________
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”…
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
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
Example: validate input, cont. What does str2double() and isnan() do?
Example when string does look like a number:
27
Example: validate input, cont. What does str2double() and isnan() do?
Example when string has an ERROR:
28
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.
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
Wrapping Up There are many built-in functions for strings
31
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
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
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