chapter 7
play

Chapter 7 Attaway MATLAB 4E Strings: Terminology A string in - PowerPoint PPT Presentation

String Manipulation Chapter 7 Attaway MATLAB 4E Strings: Terminology A string in MATLAB consists of any number of characters and is contained in single quotes strings are vectors in which every element is a single character A substring


  1. String Manipulation Chapter 7 Attaway MATLAB 4E

  2. Strings: Terminology — A string in MATLAB consists of any number of characters and is contained in single quotes — strings are vectors in which every element is a single character — A substring is a subset or part of a string — Characters include letters of the alphabet, digits, punctuation marks, white space, and control characters — Control characters are characters that cannot be printed, but accomplish a task (such as a backspace or tab) — White space characters include the space, tab, newline, and carriage return — Leading blanks are blank spaces at the beginning of a string, — Trailing blanks are blank spaces at the end of a string — Empty string is a string with length 0 , e.g. ''

  3. String Variables — String variables can be created using — assignment statements — input function (with � s � as the second argument) — Since strings are vectors of characters, many built-in functions and operators that we � ve seen already work with strings as well as numbers – e.g., length to get the length of a string, or the transpose operator — You can also index into a string variable to get individual characters or to get subsets of strings, or in other words, substrings

  4. String Concatenation — There are several ways to concatenate , or join, strings — To horizontally concatenate (creates one long string): — Using [ ], e.g. >> ['hello' 'there'] ans = hellothere — Using strcat , e.g. strcat( � hello � , � there � ) >> strcat('hello', 'there') ans = hellothere — There is a difference: if there are leading blanks, using [] will retain them whereas strcat will not

  5. Vertical Concatenation — Vertically concatenating strings creates a column vector of strings, which is basically a character matrix (a matrix in which every element is a single character) — There are 2 ways to do this: — Using [ ] and separating with semicolons — Using char — Since all rows in a matrix must have the same number of characters, shorter strings must be padded with blank spaces so that all strings are the same length ; the built-in function char will do that automatically

  6. Character Matrices — Both [ ] and char can be used to create a matrix in which every row has a string: >> cmat = ['Hello';'Hi '; 'Ciao ']; >> cmat = char('Hello', 'Hi', 'Ciao’); — Both of these will create a matrix cmat : H e l l o H i C i a o — Shorter strings are padded with blanks, e.g. cmat(2,:) is 'Hi '

  7. String Functions — String functions that deal with blank spaces: — blanks creates a string of all blank spaces (can be useful in creating blank space in output by transposing; e.g. if 5 blank lines are desired, print blanks(5)’) — deblank removes trailing blanks — strtrim removes leading and trailing blanks — (Note: no functions remove blanks in the middle of strings) — String functions that convert case for letters: — upper converts a string to all upper case letters — lower converts a string to all lower case letters

  8. The sprintf function — sprintf works just like fprintf , but instead of printing, it creates a string – so it can be used to customize the format of a string — So, sprintf can be used to create customized strings to pass to other functions (e.g., title , input ) >> maxran = randi([1, 50]); >> prompt = sprintf('Enter an integer from 1 to %d: ', maxran); >> mynum = input(prompt); Enter an integer from 1 to 46: 33 — Any time a string is required as an input, sprintf can create a customized string

  9. Example Problem We will write a function � namedept � that will receive a name and department as separate strings and will create and return a code consisting of the first two letters of the name and the last two letters of the department. The code should be upper-case letters. For example, >> namedept('Robert', 'Mechanical') ans = ROAL

  10. Example Solution function outcode = namedept(name, department) % Creates a code from a name and department % consisting of first two letters of the name % and the last two of the department outcode = upper(strcat(name(1:2), department(end-1:end))); end — Note: this is just one possible solution

  11. String Comparisons — strcmp compares two strings and returns logical 1 if they are identical or 0 if not (or not the same length) — For strings, use this instead of the equality operator == — variations: — strncmp compares only the first n characters — strcmpi ignores case (upper or lower) — strncmpi compares n characters, ignoring case

  12. Find and replace functions — strfind(string, substring) : finds all occurrences of the substring within the string; returns a vector of the indices of the beginning of the strings, or an empty vector if the substring is not found — strrep(string, oldsubstring, newsubstring) : finds all occurrences of the old substring within the string, and replaces with the new substring — the old and new substrings can be different lengths

  13. The strtok function — The strtok function takes a string and breaks it into two pieces and returns both strings — It looks for a delimiter (by default a blank space) and returns a token which is the beginning of the string up to the delimiter, and also the rest of the string, including the delimiter — A second argument can be passed for the delimiter — So – no characters are lost; all characters from the original string are returned in the two output strings — Since the function returns two strings, the call to strtok should be in an assignment statement with two variables on the left to store the two strings

  14. Examples of strtok >> mystring = 'Isle of Skye'; >> [first, rest] = strtok(mystring) first = Isle rest = of Skye >> length(rest) ans = 8 >> [f, r] = strtok(rest, 'y') f = of Sk r = ye

  15. The eval function — The eval function evaluates a string as a function call or a statement — Usually used when the contents of the string are not known ahead of time; e.g., the user enters part of it and then a customized string is created — For example: >> x = 1:5; >> fn = input('Enter a function name: ', 's'); Enter a function name: cos >> eval(strcat(fn, '(x)')) ans = 0.5403 -0.4161 -0.9900 -0.6536 0.2837

  16. eval example This is a very common application: a series of experiments has been run, resulting in files with the same name except for consecutive integers at the end of the name. We will write a for loop that will load files named � file1.dat � , � file2.dat � , … � file5.dat � (assuming that they exist) for i = 1:5 eval(sprintf('load file%d.dat',i)) end

  17. “is” & String/Number Functions — � is � functions for strings: — isletter true if the input argument is a letter of the alphabet — isspace true if the input argument is a white space character — ischar true if the input argument is a string — isstrprop determines whether the characters in a string are in a category specified by second argument, e.g. ‘alphanumeric’ — Converting from strings to numbers and vice versa: — int2str converts from an integer to a string storing the integer — num2str converts a real number to a string containing the number — str2num ( and str2double) converts from a string containing number(s) to a number array — ( Note: different from converting to/from ASCII equivalents)

  18. Common Pitfalls — Trying to use == to compare strings for equality, instead of the strcmp function (and its variations) — Confusing sprintf and fprintf . The syntax is the same, but sprintf creates a string whereas fprintf prints — Trying to create a vector of strings with varying lengths (the easiest way is to use char which will pad with extra blanks automatically) — Forgetting that when using strtok , the second argument returned (the “rest” of the string) contains the delimiter.

  19. Programming Style Guidelines — Trim trailing blanks from strings that have been stored in matrices before using — Make sure the correct string comparison function is used; for example , strcmpi if ignoring case is desired

Recommend


More recommend