A string in Matlab is merely a character row vector the size of which is the number of characters. Entering a string may be done using a sequence of characters surrounded by apostrophes.
>> strHW = 'Hello World!'
strHW =
Hello World!
>> size( strHW )
ans =
1 12
You may turn a character array into a standard Matlab array by calling the function double:
double( strHW )
>> double( strHW )
ans =
72 101 108 108 111 32 87 111 114 108 100 33
Converting a row vector v to a string does mod(floor(v), 256) and then converts each value to its ascii character equivalent.
To create a string containing an apostrophe, use a pair of apostrophes:
>> 'Winston''s home page' ans = Winston's home page
Other functions:
| Function | Description |
|---|---|
| ischar(s) | returns 1 if s is a character array, 0 otherwise. |
| isletter(s) | returns a logical 0-1 array of the same size as the string s, with an entry 1 if the corresponding entry in s is a letter (a-z, A-Z) and 0 otherwise |
| isspace(s) | returns a logical 0-1 array of the same size as the string s, with an entry 1 if the corresponding entry in s is either a spaces, newlines, carriage returns, tabs, vertical tabs, or formfeeds, and 0 otherwise |
| lower(s) | converts each lower case letter in s to upper case |
| upper(s) | converts each upper case letter in s to lower case |
| strjust(s) | change the justification (white space on either side) of the string s |
| findstr(s, t) | returns a vector of each occurence of the string t in s |
| strcmp(s, t) | returns 1 if the strings s and t are the same and 0 otherwise |
| strncmp(s, t, n) | returns 1 if the first n charcters of the strings s match the first n characters of t and 0 otherwise |
| strrep(s, t, u) | replaces all occurences of the string t in the string s with the string u |