MATLAB Programming/Strings
From Wikibooks, the open-content textbooks collection
[edit] Declaring Strings
Besides numbers, MATLAB can also manipulate strings. They should be enclosed in single quotes:
>> fstring = 'hello' fstring = hello
If you would like to include a single quote this is one way to do it:
>> fstring = '''' fstring = '
>> fstring = 'you''re' fstring = you're
An important thing to remember about strings is that MATLAB treats them as array of characters. To see this, try executing the following code:
>> fstring = 'hello'; >> class(fstring) ans = char
Therefore, many of the array manipulation functions will work the same with these arrays as any other, such as the 'size' function, transpose, and so on. You can also access specific parts of it by using standard indexing syntax.
Attempting to perform arithmetic operations on character arrays converts them into doubles.
>> fstring2 = 'world'; >> fstring + fstring2 ans = 223 212 222 216 211
These numbers come from the standard numbers for each character in the array. These values are obtained by using the 'double' function to turn the array into an array of doubles.
>> double(fstring) ans = 104 101 108 108 111
The 'char' function can turn an array of integer-valued doubles back into characters. Attempting to turn a decimal into a character causes MATLAB to round down:
>> char(104.6) ans = h >> char(104) ans = h
Since the MATLAB strings are treated as character arrays, they have some special functions if you wish to compare the entire string at once rather than just its components:
- findstr(bigstring, smallstring) looks to see if a small string is contained in a bigger string, and if it is returns the index of where the smaller string starts. Otherwise it returns [].
- strrep(string1, replaced, replacement) replaces all instances of replaced in string1 with replacement
[edit] Displaying values of string variables
If all you want to do is display the value of a string, you can omit the semicolon as is standard in MATLAB.
If you want to display a string in the command window in combination with other text you must use array notation combined with either the 'display' or the 'disp' function:
>> fstring = 'hello'; >> display( [ fstring 'world'] ) helloworld
MATLAB doesn't put the space in between the two strings. If you want one there you must put it in yourself.
This syntax is also used to concatenate two or more strings into one variable, which allows insertion of unusual characters into strings:
>> fstring = ['you' char(39) 're'] fstring = you're
Any other function that returns a string can also be used in the array.
[edit] Comparing strings
Unlike with rational arrays, strings will not be compared correctly with the relational operator, because this will treat the string as an array of characters. To get the comparison you probably intended, use the strcmp function as follows:
>> string1 = 'a'; >> strcmp(string1, 'a') ans = 1 >> strcmp(string1, 'A') ans = 0
Note that MATLAB strings are case sensitive so that 'a' and 'A' are not the same. In addition the strcmp function does not discard whitespace:
>> strcmp(string1, ' a') ans = 0
The strings must be exactly the same in every respect.
If the inputs are numeric arrays then the strcmp function will return 0 even if the values are the same. Thus it's only useful for strings. Use the == operator for numeric values.
>> strcmp(1,1) ans = 0.