




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Study notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















March 24, 2009^
Lecture 17^
C^ S^ 1 11 2
r^ o^ c^ k^ s^!
March 24, 2009^
Lecture 17^
Numerical data is often encoded in strings. E.g., a filecontaining Ithaca weather data begins with the string
meaning
Longitude:
o^76 29’ West Latitude:
o^ 26’ North
We may need to grab hold of the substring
convert
^076 and
^29 to the numeric values 76 and 29, and
March 24, 2009^
Lecture 17^
What if this nucleotide is removed?
March 24, 2009^
Lecture 17^
Some useful string functions str= ‘Cs 1112’;length(str)
% 7 isletter(str)
% [1 1 0 0 0 0 0] isspace(str)
% [0 0 1 0 0 0 0] lower(str)
% ‘cs 1112’ upper(str)
% ‘CS 1112’ ischar(str)% Is str a char array? True (1)strcmp(str(1:2),‘cs’)% Compare strings str(1:2) & ‘cs’. False (0)strcmp(str(1:3),‘CS’)% False (0)
Write a function to capitalize the first letter of each wordin a string. Assume that the string has lower case lettersand blanks only.function [str, nCaps] = caps(str)% Post: Capitalize first letter of each word.%^ str = partially capitalized string%^ nCaps = no. of capital letters% Pre: str = string with lower case letters & blanks only
look for the spacesLook For The Spaces
March 24, 2009^
Lecture 17^
March 24, 2009^
Lecture 17^
March 24, 2009^
Lecture 17^
B: the number 16C: Error in the subtraction operationD: Error in assigning variables
x1 ,^ x
E: Some other value or error
March 24, 2009^
Lecture 17^
a b c d e f g h …A B C D E F G H … Of course, do not use Matlab’s function upper!
distance
= ‘g’-‘a’ = 6 = ‘G’-‘A’
March 24, 2009^
Lecture 17^
recursion ^ Possible when result can be accumulated iteratively ^ E.g., remove all the blanks in string sSame as remove blank in s(1)
and remove blanks in s(2:length(s)) ^ E.g., capitalize first letter of all words in a sentence ^ Same as capitalize 1
st^ letter of first word and capitalize 1
st^ letter of the rest of the words
March 24, 2009^
Lecture 17^
function s = removeChar(c, s)% Return string s with character c removedif length(s)==
% Base case: nothing to do returnelse end
March 24, 2009^
Lecture 17^
function s = removeChar(c, s)% Return string s with character c removedif length(s)==
% Base case: nothing to do returnelseif s(1)~=c% return string is% s(1) and remaining s with char c removedelse endend
March 24, 2009^
Lecture 17^
function s = removeChar(c, s)% Return string s with character c removedif length(s)==
% Base case: nothing to do returnelseif s(1)~=c% return string is% s(1) and remaining s with char c removedelse% return string is just% the remaining s with char c removedendend