char, ' ' - Character array - MATLAB (original) (raw)

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.

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

Description

Create Character Vector

[C](#d126e203892) = 'text' creates a character vector of text enclosed in single quotes.

example

Convert Arrays

[C](#d126e203892) = char([A](#mw%5Fe48a8886-8c5b-4df0-b331-ea2bd6c27fbb)) 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](#d126e203892) = char(A1,...,An) converts the arrays A1,...,An into a single character array. After conversion to characters, the input arrays become rows inC. 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

Convert Dates and Times

[C](#d126e203892) = char([D](#mw%5Fb6a929df-ad5c-4f81-b696-eb80d90036db),[datefmt](#mw%5Fc7d427a2-46cd-4e34-b2eb-d3c20ae2a789)), where D is a datetime orduration array, applies the specified format, such as"HH:mm:ss".

example

[C](#d126e203892) = char([D](#mw%5Fb6a929df-ad5c-4f81-b696-eb80d90036db),[datefmt](#mw%5Fc7d427a2-46cd-4e34-b2eb-d3c20ae2a789),[locale](#mw%5Fa65e94c1-982b-4cf5-93de-b658c7d5ccf6)) specifies the locale, such as "en_US".

example

Input Arguments

expand all

Input array. The data type of A determines howchar 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. Thechar 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 orcategorical array, thenchar 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 '

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

Date or duration array, specified as a datetime,duration, or calendarDuration array. The data type of D determines howchar converts D to character vectors.

Input Type Conversion Notes Sample Input Sample Output
datetime array Converts each element to a character vector. Each element of the input array becomes a row in the new character array, automatically padded with blank spaces as needed. To specify a format and locale, see datefmt. datetime(2020,6,1) '01-Jun-2020'
duration array Converts each element to a character vector. Each element of the input array becomes a row in the new character array, automatically padded with blank spaces as needed. To specify a format and locale, see datefmt. duration(5:6,12,21) 2×8 char array '05:12:21' '06:12:21'
calendarDuration array Converts each element to a character vector. Each element of the input array becomes a row in the new character array, automatically padded with blank spaces as needed. To specify a format and locale, see datefmt. calmonths(15) + caldays(8) + hours(1.2345) '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, char uses the value in the Format property of A.

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

The supported formats depend on the data type ofA.

Locale, specified as one of these values:

To specify only the locale, use an empty array as a placeholder for the format, [].

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

Example: char(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

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

Create a character vector using single quotation marks.

Convert a numeric array to a character array.

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

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.

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 = 3×4 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.

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.

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

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 = 1×3 duration 23.134 hr 24.134 hr 25.134 hr

Convert D to a character array.

C = 3×9 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 = 3×5 char array '23:08' '24:08' '25:08'

Create a datetime.

D = datetime 01-Feb-2025 08:47:32

Convert the datetime to a character vector that is formatted and localized to france.

C = char(D,'eeee, MMMM d, yyyy HH:mm:ss',"fr_FR")

C = 'samedi, février 1, 2025 08:47:32'

Tips

Extended Capabilities

expand all

This function supports tall arrays with the limitations:

For more information, see Tall Arrays for Out-of-Memory Data.

Usage notes and limitations:

Version History

Introduced before R2006a