1 / 17

COMP 116: Introduction to Scientific Programming

COMP 116: Introduction to Scientific Programming . Lecture 24 : Strings in MATLAB. So far…. Vectors, matrices Of numbers Linear systems if- elseif -else-end Loops: while, for functions Today: letters, words, sentences characters and strings. Characters and strings.

kedma
Download Presentation

COMP 116: Introduction to Scientific Programming

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. COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB

  2. So far…. • Vectors, matrices • Of numbers • Linear systems • if-elseif-else-end • Loops: while, for • functions • Today: letters, words, sentences • characters and strings

  3. Characters and strings • Characters: ‘a’, ‘b’, ‘3’, ‘ ’ • Letters, digits, punctuation, symbols, whitespace(space, newline, … ) • Strings are sequences(vectors) of characters • ‘Meep’ is nothing but [‘M’, ‘e’, ‘e’, ‘p’] • [‘Bat’, ‘man’] gives ‘Batman’ • NOTE: Strings are rows (not columns) • ‘’ % empty string, length == 0 • ‘a’ % Single character • ‘COMP 116’

  4. Why do we need strings ? • Text Input / Output • Remember >>input(‘Give me a number:’)? • File Input / Output • Analyzing text

  5. Constructing Strings • Enclose string in single quotes • 'This is a string' • NOTE: the actual quote symbols are apostrophes • Row vector of characters • ['a', 'n', 'o', 't', 'h', 'e', 'r' ]

  6. Strings as a vector of chars • Can be manipulated like any other vector s1 = 'The quick brown fox ' s2 = 'jumped over the lazy dog' s = [s1 s2] % concatenate strings s(5) % ans = q s(17:19) % ans = fox jIdx = find( s == 'j' ) % ans=21 jStr = s(jIdx:jIdx+3) % ans = jump

  7. Exercise • Assume that str=‘this is a string’. What do the following commands do: >>str(1:4) >>str(1)==‘t’ >>str(1)=‘T’ >>str(end:-1:1)

  8. String operations • Combining • Case Conversion (upper/lower) • Conversion • Comparison • Search / Replace • Other useful functions

  9. #1: Combining strings (concatenation) • Given a, b - both string variables • Same as concatenating arrays • [ a b ] a = ‘With great power'; b = ‘comes great responsibility'; space = ' '; s = [ a space b ]

  10. #2: Case conversion • lower(varString) • Converts to lower case • upper(varString) • Converts to upper case • lower( ‘COMP 116' ) • % ans = ‘comp 116' • upper(‘I am not shouting!’)

  11. Exercise • Write a function that given a string str returns a string upper_str that is the same as str except the first character of every word in upper_str is uppercase i.e. given‘this is a string’ the function should return ‘This Is A String’ • HINT: A word starts at the first position (of the string) or after a space character • You will need for loops, if-else,==,upper

  12. #3: Comparison • Remember: strings are vectors of chars s1 = 'abcd'; s2 = 'xyzw'; s3 = 'cat'; • Avoid normal comparison operators! • s1 == s2, s1 < s3, s1 >= s3 • Operators work element by element (on characters) • Thus, strings (i.e., the vector of chars) must be same length • Use string comparison functions instead • strcmp(a,b), true when a and b are the same • strcmpi, string comparison while ignoring case • strncmp, strncmpi: • Similar, but compares first n characters only

  13. #4: Conversion: strings and numbers • num2str() • Converts numbers to strings • Handles real or complex number • str2num() • Converts strings to numbers • Handles real or complex number strings • num2str( 10.1 + 4.25i ) • % ans = ‘10.1 + 4.25i’ • disp([‘You took ’ num2str(num_guesses) ‘ tries’]); • str2num( ‘10.1 + 4.25i’ ) • % ans = 10.1 + 4.25i

  14. #5: Searching • strfind • Search for a string inside another string • returns indices to start of each instance strVal= ['with great power comes great responsibility.']; strfind( strVal, 'great') % ans = [6 24]

  15. Exercise • Write a function that given a string str, returns a string replaced_strwhich is the same as strexcept all occurrences of ‘great’ in strhave been replaced by ‘little’ • Write a function that given strings str,a,b replaces all occurrences of a in strby b. • Modify the function so that it also returns the number of replacements.

  16. Other string functions Look up what these functions do • strcat • strrep • strtok • mat2str • str2angle • strtrim • deblank • strjust • regexp • strread • textread • textscan • …

  17. Review Strings • doc strings • From MATLAB Help window, Contents tab, choose MATLAB→ProgrammingFundamentals→Characters and Strings.

More Related