Creating Character Arrays

Creating a Character String

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

whos myString
  Name          Size            Bytes  Class    Attributes

  myString      1x12               24  char               

Functions such as uint16 convert characters to their numeric codes:

str_numeric = uint16(myString)
str_numeric =

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

The char function converts the integer vector back to characters:

str_alpha = char([72 101 108 108 111 44 32 119 111 114 108 100])
str_alpha =

Hello, world

Creating a Rectangular Character Array

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.

Combining Strings Vertically

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.

Combining Strings Horizontally

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]

    MATLAB returns

    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)

    MATLAB returns

    address =

Identifying Characters in a String

Use any of the following functions to identify a character or string, or certain characters in a string:

ischarDetermine whether the input is a character array.
isletterFind all alphabetic letters in the input string.
isspaceFind all space characters in the input string.
isstrpropFind all characters of a specific category.

str = 'Find the space characters in this string';
%          |   |     |          |  |    |       
%          5   9    15         26 29   34

ans =
     5     9    15    26    29    34

Working with Space Characters

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 =

Expanding 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?