Create Character Arrays

Create Character Vector

Create a character vector by enclosing a sequence of characters in single quotation marks.

chr = 'Hello, world'
chr =

    'Hello, world'

Character vectors are 1-by-n arrays of type char. In computer programming, string is a frequently-used term for a 1-by-n array of characters. However, starting in R2016b MATLAB® also provides a string data type, so 1-by-n character arrays are referred to as character vectors in MATLAB documentation.

whos chr
  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 uint16 convert characters to their numeric codes.

chrNumeric = uint16(chr)
chrNumeric =

  1×12 uint16 row vector

    72   101   108   108   111    44    32   119   111   114   108   100

The 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'

Create Rectangular Character Array

Character arrays are m-by-n arrays of characters, where m is not always 1. 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 Creating, Concatenating, and Expanding Matrices. As with numeric arrays, you can combine character arrays vertically or horizontally to create a new character array.

However, it is recommended that you store character vectors in a cell array, instead of using m-by-n character arrays. Cell arrays are flexible containers that allow you to easily store character vectors of varying length.

Combine Character Vectors Vertically

To combine character vectors into a two-dimensional character array, use square brackets or the char function.

  • 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.']
    devTitle =
      3×13 char array
        '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.            ']
    mgrTitle =
      3×25 char array
        'Harold A. Jorgensen      '
        'Assistant Project Manager'
        'SFTware Corp.            '
  • Call the char function. If the character vectors have different lengths, char pads 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.')
    mgrTitle =
      3×25 char array
        'Harold A. Jorgensen      '
        'Assistant Project Manager'
        'SFTware Corp.            '

Combining Character Vectors Horizontally

To combine character vectors into a single row vector, use square brackets or the strcat function.

  • 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]

    MATLAB returns

    fullName =
        'Thomas R. Lee, Sr. Developer, SFTware Corp.'
  • Call the concatenation function, strcat. 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)

    MATLAB returns

    address =

Identify Characters

Use any of the following functions to identify a character array, or certain characters in a character array.

ischarDetermine whether the input is a character array
isletterFind all alphabetic letters in the input character array
isspaceFind all space characters in the input character array
isstrpropFind 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

ans =

     5     9    15    26    29    34    44

Work with Space Characters

The blanks function creates a character vector of space characters. Create a vector of 15 space characters. Character vectors always are displayed between single quotation marks.

chr = blanks(15)
chr =

    '               '

Insert a few nonspace characters in the middle of the blank character vector.

chr(6:10) = 'AAAAA'
chr =

    '     AAAAA     '

You can justify the positioning of these characters to the left or right using the strjust function:

chrLeft = strjust(chr,'left')
chrLeft =

    'AAAAA          '
chrRight = strjust(chr,'right')
chrRight =

    '          AAAAA'

Remove all trailing space characters with deblank:

chrDeblank = deblank(chr)
chrDeblank =

    '     AAAAA'

Remove all leading and trailing spaces with strtrim:

chrTrim = strtrim(chr)
chrTrim =


Expand Character Arrays

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.

See Also

| | | | |

Related Topics