Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this 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. The cellstr function removes trailing whitespace characters in the rows of S, 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 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 S.

  • Starting in R2016b, if S 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 S. Use the string function to convert back into a string array.

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. For example, cellstr(D,'HH:mm:ss') represents the times associated with each element of D.

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

Starting in R2016b, you can create string arrays using the string function. 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.

S = string('Past');
S(2) = 'Present';
S(3) = 'Future'
S = 

  1×3 string array

    "Past"    "Present"    "Future"

class(S)
ans =

string

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

C = cellstr(S)
C =

  1×3 cell array

    'Past'    'Present'    'Future'

class(C)
ans =

cell

Text processing functions (such as strfind and regexp) accept string arrays as inputs, but other functions (for example, addpath) do not.

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  

class(S)
ans =

char

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

C = cellstr(S)
C =

  3×1 cell array

    'abc'
    'defg'
    'hi'

class(C)
ans =

cell

Create a calendarDuration array.

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

  1×3 calendarDuration array

   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 =

  1×3 cell array

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

class(C)
ans =

cell

Input Arguments

collapse all

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

Starting in R2016b, S can be a string array. cellstr converts each element of S to a character vector and then assigns it to a cell of C.

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.

If S is a categorical array, then cellstr converts each element to a character vector and assigns it to a cell.

Data Types: string | char | cell | categorical

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

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

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?