180 likes | 243 Views
Announcements. Office hours 11am-12:30pm canceled However, I'll be back at 1pm (instead of 2pm), and I'll stay until 4:30pm (instead of 4pm). CS225: JAVA classes opened. Computer Science Minor?
E N D
Announcements • Office hours 11am-12:30pm canceled • However, I'll be back at 1pm (instead of 2pm), and I'll stay until 4:30pm (instead of 4pm). • CS225: JAVA classes opened. Computer Science Minor? • If any of you plan on retaking this class , with MA241 and PS150, please consider the Small Learning Community: MA 241-02, PS 150-08, EGR 115 HYB 09 & 10.
StringsCharacters and Sentences Overview Creating Strings Slicing Strings Searching for substrings
1. Strings, overview. • Strings are arrays of characters Str = 'This is 21 characters'; Spaces count as 1 characters too!
1. What are "characters"?? • ANYTHING on this keyboard is considered a single character!!! • so… what is the length() of this string? ans = _________
But what about the 1’s and 0’s? • Letters are stored internally as numbers. • The “letter to number coding” is a standardized encoding called ASCII. • Used/supported in almost every computer programming language. • Other encodings generally start with ASCII as the basis.
2. Creating Strings • Defining a string by hard-coding str = 'Fred Flintstone’; % Stored as: [70 114 101 100 32 70 108 105 110 116 115 116 111 110 101] • Creating with sprintf() str = sprintf('Curve of f(x)=%d + %dx',b,m);
Example: sprintf() • Prompt the user for the slope and y-intercept of a line, then plot. The title of the plot must reflect the current equation of the line.
Example: sprintf(), cont. • FACT: The function title() requires 1 and only 1 string argument: title( ) • This would NOT work: title('Curve of %d+%dx.', b, m) • sprintf() is used to create 1 string variable. This string variable is used in the title() command. BAD!!!
Example: sprintf(), cont. %ask for slope (m) and y-intercept (b) slope = input('Enter the slope: '); bInt = input('Enter the b-intercept: '); %create x and y data points, then plot x = [-10, 10]; %2pts only. it's a line! y = slope*x + bInt; plot(x,y) %label plot properly str = sprintf('Curve of f(x)=%d + %dx',b,m); title(str) xlabel('x') ylabel('y')
2. Creating Strings, cont. • Array-building str = 'Barney'; str = [str, ' Rubble was here']; • Concatenating: " link (things) together in a chain or series." name = 'Fred'; lastName = 'Flintstone'; str1 = [name, ' was', ' ' , 'here']; str2 = ['Hello Mr.', lastName, '. How are you?'];
2. Creating Strings, cont. • strcat() is used to combine strings, removing trailing whitespaces (trailing = end) str = strcat('Fred ','Flintstone',' was here ','! '); Expected: Fred Flintstone was here !
2. Creating Strings, cont. • strcat() is used to combine strings, removing trailing whitespaces (trailing = end) str = strcat('Fred ','Flintstone',' was here ','! '); Expected: Fred Flintstone was here ! Got: FredFlintstone was here! • It does NOT get rid of leading spaces.
3. Slicing Strings Assume: s = 'abcdefghijklmnopqrstuvwxyz'; • T = s(3:6) • X = s(1:3:10) • U = s(end:-1:22) T = ________________ X = ________________ U = _________________
Example: Extract Data • From a string, “parse” the data, i.e. extract each word separately. • Humans find the spaces first, then extract the 3 names in a split second. • MATLAB can do exactly the same!
Example: Extract Data % prompt user for full name str = input('What is your full name (first middle last)? ', 's'); % Find the spaces indices = strfind(str, ' '); %indices =find(str==' '); % Extract each separate names First = str(1:indices(1)-1) Middle = str((indices(1)+1):(indices(2)-1)) Last = str((indices(2)+1):end) strfind() returns a vector containing theindices (positions) of the desired substring ' '. Allows multiple letters as substrings.
Example: Extract Data First = str(1: indices(1)-1 ) = str(1: 5-1) = str(1:4) Middle = str(indices(1)+1 : indices(2)-1) = str( 5+1 : 10-1 ) = str(6:9) Last = str(indices(2)+1 : end) = str(10+1 : end) = str(11:end)
Example: Dates • Using the M/D/YYYYformat for a birthday parse out the: • Month • Day • Year Worksheet