cellstr

Convert to cell array of strings

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 spaces in the rows of S are removed. Use the char function to convert back into a character array.

  • If S is a cell array of strings, 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 strings 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 strings such as month and day names.

Examples

expand all

Convert Character Array to Cell Array of Strings

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

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 of Strings

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

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

expand all

S — Input arraycharacter array | cell array of strings | categorical array

Input array, specified as a character array, a cell array of strings, 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.

If S is a cell array of strings, 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 formatstring

Date format, specified as a string. 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 Display Format of Date and Time Arrays.

locale — Locale of the character strings to createstring

Locale of the character strings to create, specified as a string 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 strings 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'

Was this topic helpful?