1 / 18

Announcements

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?

chenoa
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. StringsCharacters and Sentences Overview Creating Strings Slicing Strings Searching for substrings

  3. 1. Strings, overview. • Strings are arrays of characters Str = 'This is 21 characters'; Spaces count as 1 characters too!

  4. 1. What are "characters"?? • ANYTHING on this keyboard is considered a single character!!! • so… what is the length() of this string? ans = _________

  5. 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.

  6. EVERYTHING ends up being 0' 1's

  7. 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);

  8. 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.

  9. 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!!!

  10. 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')

  11. 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?'];

  12. 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 !

  13. 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.

  14. 3. Slicing Strings Assume: s = 'abcdefghijklmnopqrstuvwxyz'; • T = s(3:6) • X = s(1:3:10) • U = s(end:-1:22) T = ________________ X = ________________ U = _________________

  15. 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!

  16. 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.

  17. 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)

  18. Example: Dates • Using the M/D/YYYYformat for a birthday parse out the: • Month • Day • Year Worksheet

More Related