Create a character vector by enclosing a sequence of characters in single quotation marks.
chr = 'Hello, world';
Character vectors are
char. In computer programming, string is
a frequently-used term for a
Name Size Bytes Class Attributes chr 1x12 24 char
If the text contains a single quotation mark, include two quotation marks when assigning the character vector.
newChr = 'You''re right'
newChr = You're right
Functions such as
characters to their numeric codes.
chrNumeric = uint16(chr)
chrNumeric = 72 101 108 108 111 44 32 119 111 114 108 100
char function converts
the integer vector back to characters.
chrAlpha = char([72 101 108 108 111 44 32 119 111 114 108 100])
chrAlpha = Hello, world
Character arrays are
of characters, where
m is not always
You can join two or more character vectors together to create a 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 character vectors into a cell array. Cell arrays are flexible containers that allow you to easily combine character vectors of varying length.
To combine character vectors into a two-dimensional character
array, use square brackets or the
Apply the MATLAB® concatenation operator,
Separate each row with a semicolon (
;). Each row
must contain the same number of characters. For example, combine three
character vectors of equal length:
devTitle = ['Thomas R. Lee'; ... 'Sr. Developer'; ... 'SFTware Corp.'];
If the character vectors have different lengths, pad with space characters as needed. For example:
mgrTitle = ['Harold A. Jorgensen '; ... 'Assistant Project Manager'; ... 'SFTware Corp. '];
char function. If the
character vectors have different lengths,
the shorter vectors with trailing blanks so that each row has the
same number of characters.
mgrTitle = char('Harold A. Jorgensen', ... 'Assistant Project Manager', 'SFTware Corp.');
char function creates a
To combine character vectors into
a single row vector, use square brackets or the
Apply the MATLAB concatenation operator,
Separate the input character vectors with a comma or a space. This
method preserves any trailing spaces in the input arrays.
name = 'Thomas R. Lee'; title = 'Sr. Developer'; company = 'SFTware Corp.'; fullName = [name ', ' title ', ' company]
fullName = Thomas R. Lee, Sr. Developer, SFTware Corp.
Call the concatenation function,
This method removes trailing spaces in the inputs. For example, combine
character vectors 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 array, or certain characters in a character array.
|Determine whether the input is a character array|
|Find all alphabetic letters in the input character array|
|Find all space characters in the input character array|
|Find all characters of a specific category|
Find the spaces in a character vector.
chr = 'Find the space characters in this character vector'; % | | | | | | | % 5 9 15 26 29 34 44 find(isspace(chr))
ans = 5 9 15 26 29 34 44
blanks function creates
a character vector of space characters. Create a vector of 15 space
chr = blanks(15)
To make the example more useful, append a
to the beginning and end of the blank character vector so that you
can see the output.
['|' chr '|']
ans = | |
Insert a few nonspace characters in the middle of the blank character vector.
chr(6:10) = 'AAAAA'; ['|' chr '|']
ans = | AAAAA |
You can justify the positioning of these characters to the left
or right using the
chrLeft = strjust(chr,'left'); ['|' chrLeft '|']
ans = |AAAAA |
chrRight = strjust(chr,'right'); ['|' chrRight '|']
ans = | AAAAA|
Remove all trailing space characters with
chrDeblank = deblank(chr); ['|' chrDeblank '|']
ans = | AAAAA|
Remove all leading and trailing spaces with
chrTrim = strtrim(chr); ['|' chrTrim '|']
ans = |AAAAA|
Generally, MathWorks® does not recommend expanding the size of an existing character array by assigning additional characters to indices beyond the bounds of the array such that part of the array becomes padded with zeros.