170 likes | 270 Views
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.
E N D
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 • 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’
Why do we need strings ? • Text Input / Output • Remember >>input(‘Give me a number:’)? • File Input / Output • Analyzing text
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' ]
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
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)
String operations • Combining • Case Conversion (upper/lower) • Conversion • Comparison • Search / Replace • Other useful functions
#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 ]
#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!’)
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
#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
#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
#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]
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.
Other string functions Look up what these functions do • strcat • strrep • strtok • mat2str • str2angle • strtrim • deblank • strjust • regexp • strread • textread • textscan • …
Review Strings • doc strings • From MATLAB Help window, Contents tab, choose MATLAB→ProgrammingFundamentals→Characters and Strings.