Presentation is loading. Please wait.

Presentation is loading. Please wait.

ES 100: Lecture 7 String Functions 1.Log in 2.Open MATLAB 3.Change the current directory and path to U: 4.Change the numeric display to compact (File,

Similar presentations


Presentation on theme: "ES 100: Lecture 7 String Functions 1.Log in 2.Open MATLAB 3.Change the current directory and path to U: 4.Change the numeric display to compact (File,"— Presentation transcript:

1 ES 100: Lecture 7 String Functions 1.Log in 2.Open MATLAB 3.Change the current directory and path to U: 4.Change the numeric display to compact (File, Preferences, Command Window) 5.Ask questions about homework & last lecture. 6.Take quiz when password provided.

2 MATLAB strings are arrays of type char: Each character is stored as two bytes, i.e., 16 bits (0’s or 1’s) >> str = 'This is a test'; >> whos Name Size Bytes Class str 1x14 28 char array Grand total is 14 elements using 28 bytes

3

4

5

6

7

8

9

10

11 ASCII Character Table

12 ASCII characters can be represented in a single byte, i.e., 8 bits (0’s or 1’s); therefore, in decimal their values range from 0 to 254 (characters beyond 127 are extended ASCII)

13 >> double('A')  Conversion of char to double float ans = 65  Note: The answer is returned in decimal (base 10) >> dec2bin(double('A'))  Conversion of decimal to binary ans = 01000001  Note: The red digit is not actually displayed >> 0*2^7+ 1*2^6+0*2^5+0*2^4+0*2^3+0*2^2+0*2^1+1*2^0 ans = 65  Hand conversion from binary (base 2) to decimal Some MATLAB examples of character conversion:

14 >> dec2hex(double('A'))  Conversion to hex ans = 41  Note: The answer is returned in base 16 >> 4*16^1+1*16^0 ans = 65  Hand conversion from hex to decimal (base 10) >> char(65)  Conversion of double float to char ans = A

15 >> str_array = char('Test_1 = 90','Test_2 = 100') str_array = Test_1 = 90  Concatenated strings despite size difference Test_2 = 100 >> double(str_array) ans = 84 101 115 116 95 49 32 61 32 57 48 32 84 101 115 116 95 50 32 61 32 49 48 48

16 >> help input INPUT Prompt for user input. R = INPUT('How many apples') gives the user the prompt in the text string and then waits for input from the keyboard. The input can be any MATLAB expression, which is evaluated, using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix. R = INPUT('What is your name','s') gives the prompt in the text string and waits for character string input. The typed input is not evaluated; the characters are simply returned as a MATLAB string…

17

18 Tips for homework problem 6.17: MATLAB functions have the following general form and are stored as function_name.m files in the path: % Data dictionary – displayed during help, if in path function output = function_name(input) output = valid_expressions(input); end

19 Tips (continued): Example string-based function % case_flip() - Flips the case of alphabetic characters % in the input string and returns this as the output string. % output_string = case_flip(input_string) function output_string = case_flip(input_string) lower_index = isstrprop(input_string,'lower'); upper_index = isstrprop(input_string,'upper'); input_string(lower_index)=upper(input_string(lower_index)); input_string(upper_index)=lower(input_string(upper_index)); output_string = input_string; end

20 Tips (continued): Example string-based function >> help case_flip case_flip() - flips the case of alphabetic characters in the input string and returns this as the output string. output_string = case_flip(input_string) >> case_flip('My test STRING!') ans = mY TEST string!

21 Do the Muddiest point, beginning with your instructor's name (not just checked). Reading assignment: Section 2.7 and Online Handout Homework: 6.12, 6.13, 6.17


Download ppt "ES 100: Lecture 7 String Functions 1.Log in 2.Open MATLAB 3.Change the current directory and path to U: 4.Change the numeric display to compact (File,"

Similar presentations


Ads by Google