cellstr

Convert to cell array of character vectors

Description

example

C = cellstr(A) converts A to a cell array of character vectors. The input array A can be a character array, a categorical array, or, starting in R2016b, a string array.

example

C = cellstr(D) converts a datetime, duration, or calendar duration array into a cell array of character vectors in the format specified by the Format property of D. The output has the same dimensions as D.

C = cellstr(D,fmt) represents dates or durations in the specified format. For example, cellstr(D,'HH:mm:ss') represents the times associated with each element of D.

C = cellstr(D,fmt,locale) represents dates or durations in the specified locale. For example, cellstr(D,'dd-MMM-yyyy','en_US') represents the dates associated with each element of D using the en_US locale. The locale affects the language used to represent character vectors such as month and day names.

Examples

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. Starting in R2017a, 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 '
    'defg'
    'hi  '

class(A)
ans = 
'char'

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

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

class(C)
ans = 
'cell'

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 14...'}    {'1y 4mo 8d 1h 14...'}    {'1y 5mo 8d 1h 14...'}

class(C)
ans = 
'cell'

Input Arguments

collapse all

Input array, specified as a character array, a cell array of character vectors, a categorical array, or a string array.

  • If A is a character array, then each row of A is a cell of C. The cellstr function removes trailing whitespace characters in the rows of A, except for significant whitespace such as nonbreaking space characters. Use the char function to convert back into a character array.

  • If A is a cell array of character vectors, then cellstr returns A unaltered.

  • If A is a categorical array, then cellstr converts each element to a character vector and assigns it to a cell. The size of the cell array is the same as the size of A.

  • Starting in R2016b, if A is a string array, then cellstr converts each element to a character vector and assigns it to a cell. The size of the cell array is the same as the size of A. Use the string function to convert back into a string array.

    • If any element of A is a missing string (displayed as <missing>), then cellstr assigns an empty character array to the corresponding cell of the output C.

Data Types: char | cell | categorical | string

Input date and time, specified as a date or duration array.

Data Types: datetime | duration | calendarDuration

Date and time format, specified as [], a character vector, or a string scalar. If you specify [], then cellstr represents input D in the format specified by the Format property of D.

The supported formats depend on the data type of D.

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

Locale represented in the output, specified as a character vector or a string scalar. The locale affects the language used to represent certain components of dates and times, such as month names.

locale can be:

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

The locale input argument can be any of the values accepted by the 'Locale' name-value pair argument for the datetime function.

Example: 'en_US'

Example: 'ja_JP'

Algorithms

cellstr does not remove trailing whitespace characters from character arrays when the characters are significant whitespace, such as nonbreaking space characters.

This table shows the most common characters that are significant whitespace characters and their descriptions. For more information, see Whitespace character.

Significant Whitespace Character

Description

char(133)

Next line

char(160)

Nonbreaking space

char(8199)

Figure space

char(8239)

Narrow no-break space

Extended Capabilities

Introduced before R2006a