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 acharacter 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 thecharfunction, described below.

Description

example

C= char(A)converts arrayAinto a character array.

example

C= char(A1,...,An)converts the arraysA1,...,Aninto a single character array. After conversion to characters, the input arrays become rows inC。Thecharfunction pads rows with blank spaces as needed. If any input array is an empty character array, then the corresponding row inCis a row of blank spaces.

The input arraysA1,...,Ancannot be string arrays, cell arrays, or categorical arrays.

A1,...,Ancan 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 theFormatproperty ofD。的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.

  • IfAis a numeric array, thencharconverts 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. Thecharfunction:

    • Rounds nonintegers toward zero.

    • Treats values less than 0 as 0.

    • Treats values greater than 65535 as 65535.

  • IfA是一个字符数组,然后呢charreturnsAunaltered.

  • IfAis a cell array of character arrays, thencharconverts the cell array to a character array. Each row from each character array in the cell array becomes a row inC, automatically padded with blank spaces as needed.

    • IfAis a multidimensional cell array, thencharcollapses the output into a two-dimensional character array. For example, ifAis a 2-by-2-by-2-by-2 cell array, then the output character arrayChas 16 rows.

  • IfAis a categorical array, thencharconverts each element ofAto a row of a character array, in column order.

  • IfAis a string array, thencharconverts the string array to a character array.charconverts each string element ofAto a character vector, and then concatenates the vectors to produce a character array, automatically padded with blank spaces as needed. Sincecharconverts 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 characterA

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[], thencharrepresents inputDin the format specified by theFormatproperty ofD

The supported formats depend on the data type ofD

  • datetimeformats can include combinations of units and delimiters, such as'yyyy-MMM-dd HH:mm:ss.SSS'。For details, see theFormat财产datetime数组。

  • durationformats 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 nineScharacters to indicate fractional second digits, such as'hh:mm:ss.SSSS'

  • calendarDurationformats can include combinations of the characters'y','q','m','w','d', and't'为了从洛杉矶rgest to the smallest unit of time, such as'ym'

For more information on thedurationandcalendarDurationformats, seeSet 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.

localecan be:

  • 'system', to specify your system locale.

  • A character vector in the formxx_YY, wherexxis a lowercase ISO 639-1 two-letter code that specifies a language, andYYis an uppercase ISO 3166-1 alpha-2 code that specifies a country.

Thelocaleinput argument can be any of the values accepted by the'Locale'name-value pair argument for thedatetimefunction.

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, seeUnicode

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

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

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,charpads the rows fromA1with blanks.

whosC
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"

ConvertAto a character vector using thecharfunction. MATLAB displays character vectors with single quotes.

C = char(A)
C = 'Pythagoras'

Create adurationarray.

D = hours(23:25) + minutes(8) + seconds(1.2345)
D =1x3 duration23.134 hr 24.134 hr 25.134 hr

ConvertDto a character array.

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

Cis a character array that represents one duration value per row.

指定的格式翅持续时间值ted byC

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

Extended Capabilities

Introduced before R2006a