This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Create Character Arrays

Create Character Vector

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

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.

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 =

     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 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.

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.'];

    If the character vectors have different lengths, pad with space characters as needed. For example:

    mgrTitle = ['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.');

    The char function creates a 3-by-25 character array mgrTitle.

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.

chr = blanks(15)
chr =


To make the example more useful, append a '|' character 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 strjust function:

chrLeft = strjust(chr,'left');
['|' chrLeft '|']
ans =

|AAAAA          |
chrRight = strjust(chr,'right');
['|' chrRight '|']
ans =

|          AAAAA|

Remove all trailing space characters with deblank:

chrDeblank = deblank(chr);
['|' chrDeblank '|']
ans =

|     AAAAA|

Remove all leading and trailing spaces with strtrim:

chrTrim = strtrim(chr);
['|' chrTrim '|']
ans =


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.

Was this topic helpful?