|On this page…|
Create a string by enclosing a sequence of letters in single quotation marks, such as
myString = 'Hello, world';
If the text contains a single quotation mark, include two quotation marks within the string definition.
otherString = 'You''re right';
In the MATLAB® computing environment, all variables are arrays, and strings are of type char (character arrays).
Name Size Bytes Class Attributes myString 1x12 24 char
Functions such as uint16 convert characters to their numeric codes:
str_numeric = uint16(str)
str_numeric = 72 101 108 108 111
The char function converts the integer vector back to characters:
str_alpha = char([72 101 108 108 111])
str_alpha = Hello
You can join two or more strings together to create a new character array. This is called concatenation and is explained for numeric arrays in the section Concatenating Matrices. As with numeric arrays, you can combine character arrays vertically or horizontally to create a new character array.
Alternatively, combine strings into a cell array. Cell arrays are flexible containers that allow you to easily combine strings of varying length.
To combine strings into a two-dimensional character array, use either of these methods:
Apply the MATLAB concatenation operator, . Separate each row with a semicolon (;). Each row must contain the same number of characters. For example, combine three strings of equal length:
dev_title = ['Thomas R. Lee'; ... 'Sr. Developer'; ... 'SFTware Corp.'];
If the strings have different lengths, pad with space characters as needed. For example:
mgr_title = ['Harold A. Jorgensen '; ... 'Assistant Project Manager'; ... 'SFTware Corp. '];
Call the char function. If the strings are different length, char pads the shorter strings with trailing blanks so that each row has the same number of characters. For example, combine three strings of different lengths:
mgr_title = char('Harold A. Jorgensen', ... 'Assistant Project Manager', 'SFTware Corp.');
The char function creates a 3-by-25 character array mgr_title.
To combine strings into a single row vector, use either of these methods:
Apply the MATLAB concatenation operator, . Separate the input strings with a comma or a space. This method preserves any trailing spaces in the input arrays. For example, combine several strings:
name = 'Thomas R. Lee'; title = 'Sr. Developer'; company = 'SFTware Corp.'; full_name = [name ', ' title ', ' company]
full_name = Thomas R. Lee, Sr. Developer, SFTware Corp.
Call the string concatenation function, strcat. This method removes trailing spaces in the inputs. For example, combine strings to create a hypothetical email address:
name = 'myname '; domain = 'mydomain '; ext = 'com '; address = strcat(name, '@', domain, '.', ext)
address = firstname.lastname@example.org
Use any of the following functions to identify a character or string, or certain characters in a string:
|ischar||Determine whether the input is a character array.|
|isletter||Find all alphabetic letters in the input string.|
|isspace||Find all space characters in the input string.|
|isstrprop||Find all characters of a specific category.|
str = 'Find the space characters in this string'; % | | | | | | % 5 9 15 26 29 34 find(isspace(str)) ans = 5 9 15 26 29 34
The blanks function creates a string of space characters. The following example creates a string of 15 space characters:
s = blanks(15) s =
To make the example more useful, append a '|' character to the beginning and end of the blank string so that you can see the output:
['|' s '|'] % Make result visible. ans = | |
Insert a few nonspace characters in the middle of the blank string:
s(6:10) = 'AAAAA'; ['|' s '|'] % Make result visible. ans = | AAAAA |
You can justify the positioning of these characters to the left or right using the strjust function:
sLeft = strjust(s, 'left'); ['|' sLeft '|'] % Make result visible. ans = |AAAAA | sRight = strjust(s, 'right'); ['|' sRight '|'] % Make result visible. ans = | AAAAA|
Remove all trailing space characters with deblank:
sDeblank = deblank(s); ['|' sDeblank '|'] % Make result visible. ans = | AAAAA|
Remove all leading and trailing spaces with strtrim:
sTrim = strtrim(s); ['|' sTrim '|'] % Make result visible. ans = |AAAAA|