Documentation

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.

cellstr

Convert to cell array of character vectors

Syntax

  • S = cellstr(D)
    example
  • S = cellstr(D,fmt)
  • S = cellstr(D,fmt,locale)

Description

example

C = cellstr(S) converts array S to a cell array.

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

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

  • If S is a categorical array, then cellstr returns a cell array of the same size.

example

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

S = cellstr(D,fmt) represents dates or durations in the specified format, such as 'HH:mm:ss'.

S = cellstr(D,fmt,locale) represents dates or durations in the specified locale, such as 'en_US'. The locale affects the language used to represent character vectors such as month and day names.

Examples

collapse all

Convert Character Array to Cell Array

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

S = ['abc ';'defg';'hi  ']
S =

abc 
defg
hi  

whos S
  Name      Size            Bytes  Class    Attributes

  S         3x4                24  char               

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

C = cellstr(S)
C = 

    'abc'
    'defg'
    'hi'

whos C
  Name      Size            Bytes  Class    Attributes

  C         3x1               354  cell               

Convert Calendar Duration Array to Cell Array

Create a calendarDuration array.

D = calmonths(15:17) + caldays(8) + hours(1.2345)
D = 

   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 = 

    '1y 3mo 8d 1h 14m ...'    '1y 4mo 8d 1h 14m ...'    '1y 5mo 8d 1h 14m ...'

whos C
  Name      Size            Bytes  Class    Attributes

  C         1x3               462  cell               

Input Arguments

collapse all

S — Input arraycharacter array | cell array of character vectors | categorical array

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

If S is a character array, then each row of S becomes a separate cell of C, with trailing spaces removed. cellstr does not remove trailing spaces that are nonbreaking space characters.

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

Data Types: char | cell

D — Input date and timedate or duration array

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

Data Types: datetime | duration | calendarDuration

fmt — Date and time format[] (default) | character vector

Date and time format, specified as a character vector. The supported formats depend on the data type of input 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 — Locale of the character vectors to createcharacter vector

Locale of the character vectors to create, specified as a character vector.

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.

These are the same character vectors accepted by the 'Locale' name-value pair argument for the datetime function. The locale affects the language used in the output array.

Example: 'en_US'

Example: 'ja_JP'

More About

collapse all

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

Introduced before R2006a

Was this topic helpful?