Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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

### Syntax

``C = char(A)``
``C = char(A1,...,An)``
``C = char(D)``
``C = char(D,fmt)``
``C = char(D,fmt,locale)``

### Description

example

````C = char(A)` converts array `A` into a character array.```

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(D)` converts a datetime, duration, or calendar duration array into a character array in the format specified by the `Format` property of `D`. The output contains one date or duration in each row.```

example

````C = char(D,fmt)` represents dates or durations in the specified format, such as `'HH:mm:ss'`.```
````C = char(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.```

### Input Arguments

expand all

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

• If `A` is a numeric array, then `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.

• If `A` is a character array, then `char` returns `A` unaltered.

• If `A` is a cell array of character arrays, then `char` converts the cell array to a character array. Each row from each character array in the cell array becomes a row in `C`, automatically padded with blank spaces as needed.

• If `A` is a multidimensional cell array, then `char` collapses the output into a two-dimensional character array. For example, if `A` is a 2-by-2-by-2-by-2 cell array, then the output character array `C` has 16 rows.

• If `A` is a categorical array, then `char` converts each element of `A` to a row of a character array, in column order.

• If `A` is a string array, then `char` converts the string array to a character array. `char` converts each string element of `A` to a character vector, and then concatenates the vectors to produce a character array, automatically padded with blank spaces as needed. Since `char` converts each string to a character vector, the size of the output character array is different from the size of the string array.

Example: `char(65)` converts the integer 65 to the character `A`.

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

Data Types: `datetime` | `duration` | `calendarDuration`

Date and time format, specified as `[]`, a character vector, or a string scalar. If you specify `[]`, then `char` represents input `D` in the format specified by the `Format` property of `D`.

The supported formats depend on the data type of `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 the largest to the smallest unit of time, such as `'ym'`.

For more information on the `duration` and `calendarDuration` formats, see Set Date and Time Display Format.

Locale represented in the output, specified as a character vector or a string scalar. The locale affects the language used to represent certain components of dates and times, such as month names.

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

The `locale` input argument can be any of the values accepted by the `'Locale'` name-value pair argument for the `datetime` function.

Example: `'en_US'`

Example: `'ja_JP'`

### 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. Starting in R2017a, 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' ```

Create a `duration` array.

`D = hours(23:25) + minutes(8) + seconds(1.2345)`
```D = 1x3 duration array 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' ```

Download ebook