SUNY Geneseo Department of Mathematics
Monday, April 24
Math 230 02
Spring 2017
Prof. Doug Baldwin
Available in “Exam 2 Solutions” folder in the “Files” area in Canvas. Or see...
Speaker Thursday (April 27), 5:15, Newton 214
Reginald Byron, Southwestern University and Geneseo Alum
“Implicit Bias, Microaggressions, and the March Toward More Inclusive Pedagogies”
You get paid (with $1 added to your printing balance) for completing SOFIs! (And thanks to those of you who have already completed them for this course.)
Sections 7.1 and 7.2
What happens if you try to reply to an input
command with a string? Alternatively,
how do you reply to an input
command with a string?
input
turns out to be more complicated than it might look. It normally tries
to interpret anything you enter as a Matlab expression to evaluate, so strings have to be
enclosed in single quotation marks. But adding a second argument of 's'
to
input
tells it to always treat all input as a string, without quotation marks.>> input( 'Enter a string ' )
Enter a string 'good morning'
ans =
good morning
>> input( 'What is your name? ' )
What is your name? Joe
Error using input
Undefined function or variable 'Joe'.
What is your name? 3*sin(pi) - 17^3
ans =
-4913
>> input( 'What is your name? ', 's' )
What is your name? Joe
ans =
Joe
>> input( 'What is your name? ', 's' )
What is your name? 3* sin(pi) - 17^3
ans =
3* sin(pi) - 17^3
Write a script that prompts for and inputs a description of today’s weather, and then creates a string of the form “I’m glad it’s <description of weather> today.”
Relevant ideas or questions from the reading:
uint8
, codes, etc?
uint16
(uint16
is actually preferable to the uint8
described by the book) translates characters
to numeric codes, while char
translates the other way.>> s = 'ABCabc 012 &*. Σ'
s =
ABCabc 012 &*. Σ
>> uint16( s )
ans =
Columns 1 through 7
65 66 67 97 98 99 32
Columns 8 through 14
48 49 50 32 38 42 46
Columns 15 through 16
32 8721
>> char( 6 ) % 6 doesn't correspond to a printable character
ans =
>> char( 97 )
ans =
a
>> char( [ 89, 3412, 87, 999 ] )
ans =
YൔWϧ
How do you get a single quotation mark into a string?
>> 'I''m glad'
ans =
I'm glad
Solution (in script file “weather.m”):
word = 'sunny';
phrase1 = 'I''m glad it''s';
phrase2 = 'today';
sentence = [ phrase1, blanks(1), word, blanks(1), phrase2 ]
Running this produces:
>> weather sentence = I'm glad it's sunny today
Comments:
input
commands with the 's'
option, or by concatenating shorter
strings together.blanks
function to insert spacesExtend the script from above so that it generates different strings for different kinds of weather, e.g., “I’m sorry....” if the weather is “rainy,” “I’m glad...” if the weather is “sunny,” maybe a couple of others.
Relevant ideas or questions from the reading:
strcmp
family of functions (strcmp
, strcmpi
,
strncmp
, strncmpi
) lets you test whether strings are equal to each other.>> strcmp( word, 'rainy' )
ans =
0
>> word
word =
warm and sunny
>> strcmp( word, 'warm and sunny' )
ans =
1
if
statement.Solutions (still in script file “weather.m”):
word = input( 'Weather description: ', 's' );
if strcmp( word, 'rainy' )
fprintf( 'I''m sorry it''s rainy today\n' );
elseif strcmp( word, 'sunny' )
fprintf( 'That''s nice!\n' );
elseif strcmpi( word, 'sunny' )
fprintf( 'I''m glad\n' );
else
fprintf( 'I''ve never seen %s weather\n', word )
end
Running this now produces interactions such as...
>> weather
Weather description: sunny
That's nice!
>> weather
Weather description: SUNNY
I'm glad
>> weather
Weather description: foggy
I've never seen foggy weather
>> weather
Weather description: sunny
That's nice!
Comments:
strcmp
family of functions is a good way to compare strings, with
options for making the comparison case-sensitive or -insensitive and using all the characters
in the strings or only some.fprintf
by using the %s
directive.The string construction, comparison, and output operations discussed here allow you to do a great deal of text processing in Matlab.
You can do even more by remembering that strings are really vectors of integers, and using standard vector operations on them.
Cell arrays
Read textbook sections 14.1 through 14.3