Main Content


Convert to cell array of character vectors



C = cellstr(A) converts A to a cell array of character vectors. For instance, if A is a string, "foo", C is a cell array containing a character vector, {'foo'}.


C = cellstr(A, dateFmt), where A is a datetime or duration array, applies the specified format, such as "HH:mm:ss". Additionally, you can specify the locale as a separate input, such as "en_US".


collapse all

You can create string arrays to contain multiple pieces of text. However, you might need to use functions that accept cell arrays of character vectors as input arguments, and that do not accept string arrays. To pass data from a string array to such functions, use the cellstr function to convert the string array to a cell array of character vectors.

Create a string array. You can create strings using double quotes.

A = ["Past","Present","Future"]
A = 1x3 string
    "Past"    "Present"    "Future"

Convert the string array to a 1-by-3 cell array of character vectors.

C = cellstr(A)
C = 1x3 cell
    {'Past'}    {'Present'}    {'Future'}

Create a character array. Include trailing spaces so that each row has the same length, resulting in a 3-by-4 array.

A = ['abc ';'defg';'hi  ']
A = 3x4 char array
    'abc '
    'hi  '

ans = 

Convert the character array to a 3-by-1 cell array of character vectors.

C = cellstr(A)
C = 3x1 cell
    {'abc' }
    {'hi'  }

ans = 

Create a calendarDuration array.

D = calmonths(15:17) + caldays(8) + hours(1.2345)
D = 1x3 calendarDuration
   1y 3mo 8d 1h 14m 4.2s   1y 4mo 8d 1h 14m 4.2s   1y 5mo 8d 1h 14m 4.2s

Convert the array to a cell array of character vectors.

C = cellstr(D)
C = 1x3 cell
    {'1y 3mo 8d 1h 14m 4.2s'}    {'1y 4mo 8d 1h 14m 4.2s'}    {'1y 5mo 8d 1h 14m 4.2s'}

ans = 

Input Arguments

collapse all

Input array. The data type of A determines how cellstr converts A to a cell array of character vectors.

Input Type

Conversion Notes

Sample Input

Sample Output


Converts each element to a character vector and assigns it to a cell.

If A is empty, "", the output is a cell containing an empty character array, a 0-by-0 character vector.

1×1 string array

1×1 cell array
1×2 string array
    "foo"    "bar"
1×2 cell array
    {'foo'}    {'bar'}

Character arrays

Assigns each row of the input to a cell. cellstr removes trailing whitespace characters in each row, except for significant whitespace such as nonbreaking space characters.

2×3 char array
 2×1 cell array

Categorical array

Converts each element of the input array to a character vector and assigns the vector to a cell in the new cell array.

1x3 categorical array
    red    green    blue
1×3 cell array
    {'red'}    {'green'}    {'blue'}

datetime array

To specify a format and locale, see dateFmt.



Date format and locale, specified as separate character vectors or string scalars. Input A must be of type datetime, duration, or calendarDuration.

If you do not specify a format, cellstr uses the value in the Format property of A. To specify only the locale, use an empty array as a placeholder for the format, [].

Example: cellstr(A, "yyyy-MM-dd")

Example: cellstr(A, "yyyy-MM-dd","en_US")

Example: cellstr(A, [],"en_US")

The supported formats depend on the data type of A.

  • datetime formats can include combinations of units and delimiters, such as "yyyy-MMM-dd HH:mm:ss.SSS". For details, see the Format property for datetime arrays.

  • duration formats are either single characters (y, d, h, m, or s) or one of these combinations:

    • "dd:hh:mm:ss"

    • "hh:mm:ss"

    • "mm:ss"

    • "hh:mm"

    • Any of the above, with up to nine S characters to indicate fractional second digits, such as "hh:mm:ss.SSSS"

  • calendarDuration formats can include combinations of the characters y, q, m, w, d, and t in order from largest to smallest unit of time, such as "ym". For more information on the duration and calendarDuration formats, see Set Date and Time Display Format.

The locale affects the language used to represent certain components of dates and times, such as month names. Valid values are:

  • "system", to specify your system locale.

  • A character vector in the form xx_YY, where xx is a lowercase ISO 639-1 two-letter code that specifies a language, and YY is an uppercase ISO 3166-1 alpha-2 code that specifies a country. For sample values, see the Locale name-value argument for the datetime function.

Extended Capabilities

Version History

Introduced before R2006a