cellstr - Convert to cell array of character vectors - MATLAB (original) (raw)
Convert to cell array of character vectors
Syntax
Description
Convert Arrays
C = cellstr([A](#mw%5F43b42697-0729-44e3-b258-ed8bfcf96a3e))
convertsA
to a cell array of character vectors. For instance, ifA
is a string, "foo"
,C
is a cell array containing a character vector,{'foo'}
.
Convert Dates and Times
`C` = cellstr([D](#mw%5Fa9211254-61ed-4956-8f8a-c4ff5b344e58),[datefmt](#mw%5Fcb173ced-594a-40c2-b2be-cf034c400538))
, where D
is a datetime
orduration
array, applies the specified format, such as"HH:mm:ss"
.
`C` = cellstr([D](#mw%5Fa9211254-61ed-4956-8f8a-c4ff5b344e58),[datefmt](#mw%5Fcb173ced-594a-40c2-b2be-cf034c400538),[locale](#mw%5F9bfa804d-1ca5-459e-a700-852e48076afb))
specifies the locale, such as "en_US"
.
Examples
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 = 1×3 string "Past" "Present" "Future"
Convert the string array to a 1-by-3 cell array of character vectors.
C = 1×3 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 = 3×4 char array 'abc ' 'defg' 'hi '
Convert the character array to a 3-by-1 cell array of character vectors.
C = 3×1 cell {'abc' } {'defg'} {'hi' }
Create a calendarDuration
array.
D = calmonths(15:17) + caldays(8) + hours(1.2345)
D = 1×3 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 = 1×3 cell {'1y 3mo 8d 1h 14m 4.2s'} {'1y 4mo 8d 1h 14m 4.2s'} {'1y 5mo 8d 1h 14m 4.2s'}
Create a datetime
.
D = datetime 01-Feb-2025 08:47:31
Convert the datetime
to a character vector that is formatted and localized to france.
C = cellstr(D,'eeee, MMMM d, yyyy HH:mm:ss',"fr_FR")
C = 1×1 cell array {'samedi, février 1, 2025 08:47:31'}
Input Arguments
Input array. The data type of A
determines howcellstr
converts A
to a cell array of character vectors.
Input Type | Conversion Notes | Sample Input | Sample Output |
---|---|---|---|
string | 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 "foo" | 1×1 cell array {'foo'} |
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 'foo' 'bar' | 2×1 cell array {'foo'} {'bar'} |
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'} |
Date or duration array, specified as a datetime
,duration
, or calendarDuration
array. The data type of D
determines howcellstr
converts A
to a cell array of character vectors.
Input Type | Conversion Notes | Sample Input | Sample Output |
---|---|---|---|
datetime array | Converts each element to a character vector and assigns it to a cell. To specify a format and locale, see datefmt. | datetime(2020,6,1) | 1x1 cell array {'01-Jun-2020'} |
duration array | Converts each element to a character vector and assigns it to a cell. To specify a format and locale, see datefmt. | duration(5:6,12,21) | 1x2 cell array {'05:12:21'} {'06:12:21'} |
calendarDuration array | Converts each element to a character vector and assigns it to a cell. To specify a format and locale, see datefmt. | calmonths(15) + caldays(8) + hours(1.2345) | 1x1 cell array {'1y 3mo 8d 1h 14m 4.2s'} |
Date format and locale, specified as separate character vectors or string scalars. Input A
must be of typedatetime
, duration
, orcalendarDuration
.
If you do not specify a format, cellstr
uses the value in the Format
property of A
.
Example: cellstr(A,"yyyy-MM-dd")
Example: cellstr(A,"dd:hh:mm:ss","en_US")
The supported formats depend on the data type ofA
.
datetime
formats can include combinations of units and delimiters, such as"yyyy-MMM-dd HH:mm:ss.SSS"
. For details, see the Format property fordatetime
arrays.duration
formats are either single characters (y
,d
,h
,m
, ors
) 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 charactersy
,q
,m
,w
,d
, andt
in order from largest to smallest unit of time, such as"ym"
. For more information on theduration
andcalendarDuration
formats, see Set Date and Time Display Format.
Locale, specified as one of these values:
"system"
, to specify your system locale.- A string scalar 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.
To specify only the locale, use an empty array as a placeholder for the format, []
.
Example: cellstr(A, "yyyy-MM-dd","en_US")
Example: cellstr(A, [],"en_US")
The locale affects the language used to represent certain components of dates and times, such as month names. Valid values are:
This table lists some common values for the locale.
Locale | Language | Country |
---|---|---|
"de_DE" | German | Germany |
"en_GB" | English | United Kingdom |
"en_US" | English | United States |
"es_ES" | Spanish | Spain |
"fr_FR" | French | France |
"it_IT" | Italian | Italy |
"ja_JP" | Japanese | Japan |
"ko_KR" | Korean | Korea |
"nl_NL" | Dutch | Netherlands |
"zh_CN" | Chinese (simplified) | China |
Extended Capabilities
Version History
Introduced before R2006a