You are on page 1of 4

CS1112 Lecture 17

Previous Lecture:

Characters & strings

Working with images

Todays Lecture:

We have used strings already: n= input(Next number: ) sprintf(Answer is %d, ans) A string is made up of individual characters, so a string is a 1-d array of characters CS1112 rocks! is a character array of length 13; it has 7 letters, 4 digits, 1 space, and 1 symbol.
C S 1 1 1 2 r o c k s !

Characters and strings

Announcements:

Discussion this week in classrooms as listed on roster Project 4 due Thurs 3/28 at 11pm

Can have 2-d array of characters as well


C S 1 1 1 2 r o c k s !
26 matrix

Matlab types: char, double, uint8, logical


There is not a type string! What we call a string is a 1-d array of chars

Strings are important in computation


Numerical data is often encoded in strings. E.g., a file containing Ithaca weather data begins with the string W07629N4226 meaning Longitude: 76o 29 West Latitude: 42o 26 North We may need to grab hold of the substring W07629, convert 076 and 29 to the numeric values 76 and 29, and do some computation
Lecture 17 5

a C S 1

a is a 1-d array with type char components. We call a a string or char array b is a 1-d array with type double components. double is the default type for numbers in Matlab. We call b a numeric array c is a 1-d array with type uint8 components. We call c a uint8 array d is a scalar of the type logical. We call d a boolean value
Lecture 17 4

b = [3 9]

c = uint8(b)

d = rand > .5

Comparison of genomic sequences is another example of string computation

Single quotes enclose strings in Matlab Anything enclosed in single quotes is a string (even if it looks like something else)

E.g., looking for a pattern: Given the sequence ATTCTGACCTCGATC Look for the pattern ACCT E.g., quantifying the difference between sequences: ATTCTGACCTCGATC ATTCGTGACCTCGAT

100 is a character array (string) of length 3 100 is a numeric value pi is a character array of length 2 pi is the built-in constant 3.1416 x is a character (vector of length 1) x may be a variable name in your program

Lecture 17

Lecture 17

Lecture slides

CS1112 Lecture 17

Strings are vectors

Vectors Assignment
v= [7 0 5];

Strings Assignment
s= hello;

Some useful string functions


str= Cs 1112; length(str) isletter(str) isspace(str) lower(str) upper(str) % % % % % 7 [1 1 0 0 0 0 0] [0 0 1 0 0 0 0] cs 1112 CS 1112

Indexing
x= v(3); % x is 5 v(1)= 1; % v is [1 0 5] w= v(2:3); % w is [0 5]

Indexing
c= s(2); % c is e s(1)= J; % s is Jello t= s(2:4); % t is ell

: notation
v= 2:5; % v is [2 3 4 5]

: notation
s= a:g; % s is abcdefg

Appending
v= [7 0 5]; v(4)= 2; % v is [7 0 5 2]

Appending
s= duck; s(5)= s; % s is ducks

Concatenation
v= [v [4 6]]; % v is [7 0 5 2 4 6]

Concatenation
s= [s quack]; % s is ducks quack

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)
8 Lecture 17 9

Lecture 17

Example: capitalize 1st letter


Write a function to capitalize the first letter of each word in a string. Assume that the string has lower case letters and blanks only. (OK to use built-in function upper) 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 Look For the The spaces Spaces
See caps.m

ASCII characters
(American Standard Code for Information Interchange)

ascii code : : 65 66 67 : 90 :

Character : : A B C : Z :

ascii code : : 48 49 50 : 57 :
Lecture 17

Character : : 0 1 2 : 9 :
11

Character vs ASCII code

Arithmetic and relational ops on characters


str= Age 19 %a 1-d array of characters code= double(str) %convert chars to ascii values str1= char(code) %convert ascii values to chars

c-a gives 2 6-5 gives 1 letter1=e; letter2=f; letter1-letter2 gives -1 c>a gives true letter1==letter2 gives false A + 2 gives 67 char(A+2) gives C
Lecture 17 13


Lecture 17 12

Lecture slides

CS1112 Lecture 17

What is in variable g (if it gets created)? d1= Mar 3; d2= Mar 9; x1= d1(5); x2= d2(5); g= x2-x1;
A: the character 6 B: the numeric value 6 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error
Lecture 17 14

What is in variable g (if it gets created)? d1= Mar 13; d2= Mar 29; x1= d1(5:6); x2= d2(5:6); g= x2-x1;
A: the string 16 B: the numeric value 16 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error
Lecture 17 15

Example: toUpper
Write a function toUpper(cha) to convert character cha to upper case if cha is a lower case letter. Return the converted letter. If cha is not a lower case letter, simply return the character cha. Hint: Think about the distance between a letter and the base letter a (or A). E.g., a b c d e f g h
distance = g-a = 6 = G-A function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha.

A B C D E F G H Of course, do not use Matlab function upper!


Lecture 17 16 Lecture 17 17

Example: censoring words


function D = censor(str, A) % Replace all occurrences of string str in % character matrix A with Xs, regardless of % case. % Assume str is never split across two lines. % D is A with Xs replacing str. Us e M ATX XX in th at XXX . D

function D = censor(str, A) % Replace all occurrences of string str in character matrix A, % regardless of case, with X's. % A is a matrix of characters. % str is a string. Assume that str is never split across two lines. % D is A with X's replacing the censored string str. D= A; B= lower(A); s= lower(str); ns= length(str); [nr,nc]= size(A); % Build a string of X's of the right length

% Traverse the matrix to censor string str

A Us e M AT LA B in t h at l ab.

Lecture 18

22

Lecture 17

23

Lecture slides

CS1112 Lecture 17

function D = censor(str, A) % Replace all occurrences of string str in character matrix A, % regardless of case, with X's. % A is a matrix of characters. % str is a string. Assume that str is never split across two lines. % D is A with X's replacing the censored string str. D= A; B= lower(A); s= lower(str); ns= length(str); [nr,nc]= size(A); % Build a string of X's of the right length Xs= char( zeros(1,ns)); for k= 1:ns Xs(k)= 'X'; end % Traverse the matrix to censor string str

Example: removing all occurrences of a character

From a genome bank we get a sequence ATTG CCG TA GCTA CGTACGC AACTGG AAATGGC CGTAT First step is to clean it up by removing all the blanks. Write this function:
function s = removeChar(c, s) % Return string s with all occurrences % of character c removed

zeros returns an array of type double

Lecture 17

24

Lecture 17

27

Example: removing all occurrences of a character


Can solve this problem using iterationcheck one character (one component of the vector) at a time
function s = removeChar_loop(c, s) % Return string s with all occurrences of % character c removed.

Example: removing all occurrences of a character


Can solve this problem using iterationcheck one character (one component of the vector) at a time
function s = removeChar_loop(c, s) % Return string s with all occurrences of % character c removed. t= ; for k= 1:length(s)

end s= t;
Lecture 17 28 Lecture 17 29

Lecture slides

You might also like