Main Content

char

Character array

Description

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.

Creation

You can create a character vector using single quotation marks.

C = 'Hello, world'
C =

    'Hello, world'

If you have an array of a different data type, you can convert it to a character array using the char function, described below.

Description

example

C = char(A) converts the input array, A, to a character array. For instance, if A is a string, "foo", c is a character array, 'foo'.

example

C = char(A1,...,An) converts the arrays A1,...,An into a single character array. After conversion to characters, the input arrays become rows in C. The char function pads rows with blank spaces as needed. If any input array is an empty character array, then the corresponding row in C is a row of blank spaces.

The input arrays A1,...,An cannot be string arrays, cell arrays, or categorical arrays.

A1,...,An can be of different sizes and shapes.

example

c = char(A, dateFmt), where A is a datetime or duration array, applies the specified format, such as "HH:mm:ss". Additionally, you can specify the locale as a separate input, such as "en_US".

Input Arguments

expand all

Input array. The data type of A determines how char converts A to a character array.

Input Type

Conversion Notes

Sample Input

Sample Output

string

Each element of the input array becomes a row in the new character array, automatically padded with blank spaces as needed.

If A is empty, "", the output is an empty character array, a 0-by-0 character vector.

1×1 string array
    "foo"

1×3 char array
    'foo'
2×1 string array
    "foo"
    "bar"
2×3 char array
    'foo'
    'bar'

Numeric array

char converts numbers into characters. Valid numeric values range from 0 to 65535 and correspond to Unicode® code units. Values from 0 to 127 also correspond to 7-bit ASCII characters. The char function:

  • Rounds nonintegers toward zero.

  • Treats values less than 0 as 0.

  • Treats values greater than 65535 as 65535.

[102 111 111 33 ]

'foo!'

Cell array of character vectors

If the input is a cell array of character vectors or categorical array, then char converts the input to a character array. Each row from each element of the input array becomes a row in the new character array, automatically padded with blank spaces as needed.

{'foo','bar'}

2×3 char array

    'foo'
    'bar'

Categorical array

1x3 categorical array
    red    green    blue
3×5 char array

    'red  '
    'green'
    'blue '

datetime array

To specify a format and locale, see dateFmt.

datetime(2020,6,1)

'01-Jun-2020'

Converted missing values, such as NaN, NaT, and <undefined> categorical values, display as <missing>.

Date format and locale, specified as separate character vectors or string scalars. Input A must be of type datetime, duration, or calendarDuration.

If you do not specify a format, char uses the value in the Format property of A. To specify only the locale, use an empty array as a placeholder for the format, [].

Example: char(A, "yyyy-MM-dd")

Example: char(A, "yyyy-MM-dd","en_US")

Example: char(A, [],"en_US")

The supported formats depend on the data type of A.

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

The locale affects the language used to represent certain components of dates and times, such as month names. Valid values are:

  • "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. For sample values, see the Locale name-value argument for the datetime function.

Output Arguments

expand all

Output array, returned as a character array. Character arrays can have any size, but their most typical use is for storing pieces of text as character vectors.

MATLAB® stores all characters as Unicode characters using the UTF-16 encoding. For more information on Unicode, see Unicode.

Examples

collapse all

Convert a numeric array to a character array.

A = [77 65 84 76 65 66];
C = char(A)
C = 
'MATLAB'

The integers from 32 to 127 correspond to printable ASCII characters. However, the integers from 0 to 65535 also correspond to Unicode® characters. You can convert integers to their corresponding Unicode representations using the char function.

For example, the number 8451 corresponds to the symbol for degrees Celsius. Convert 8451 using char.

C = char(8451)
C = 
'℃'

Convert multiple arrays into a single character array. The input arrays need not have the same shape.

A1 = [65 66; 67 68];
A2 = 'abcd';
C = char(A1,A2)
C = 3x4 char array
    'AB  '
    'CD  '
    'abcd'

Because the input arrays do not have the same number of columns, char pads the rows from A1 with blanks.

whos C
  Name      Size            Bytes  Class    Attributes

  C         3x4                24  char               

Create a string scalar. You can create string scalars using double quotes. MATLAB® also displays strings with double quotes.

A = "Pythagoras"
A = 
"Pythagoras"

Convert A to a character vector using the char function. MATLAB displays character vectors with single quotes.

C = char(A)
C = 
'Pythagoras'

Convert from a duration array to char. For more information related to converting from common data types to char see Convert Between Text and datetime or duration Values.

Create a duration array.

D = hours(23:25) + minutes(8) + seconds(1.2345)
D = 1x3 duration
   23.134 hr   24.134 hr   25.134 hr

Convert D to a character array.

C = char(D)
C = 3x9 char array
    '23.134 hr'
    '24.134 hr'
    '25.134 hr'

C is a character array that represents one duration value per row.

Specify the format of the duration values represented by C.

C = char(D,'hh:mm')
C = 3x5 char array
    '23:08'
    '24:08'
    '25:08'

Tips

  • Converting a char array to a numeric type will produce an array of the corresponding Unicode code values. Text in strings does not convert in this way. Converting a string that does not represent a single numeric value to double will produce a NaN result. For more information, see Unicode and ASCII Values.

Extended Capabilities

Version History

Introduced before R2006a