Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

char

Convert to character array

Syntax

Description

example

S = char(A) converts array A into a character 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.

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

    To convert characters into a numeric array, use a function that converts to a numeric type (for example, double, int32, or cast).

  • If A is a cell array of character vectors, then char converts the cell array into a character array. The character vectors from the cell array become rows in S (automatically padded with blanks as needed).

    To convert S back into a cell array, use the cellstr function.

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

example

S = char(A1,...,AN) converts the arrays A1,...,AN into a single character array. After conversion to characters, the input arrays become rows in S. Each row is automatically padded with blanks as needed. An empty character vector becomes a row of blanks.

The input arrays A1,...,AN cannot be cell arrays or categorical arrays.

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

example

S = 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

S = char(D,fmt) represents dates or durations in the specified format, such as 'HH:mm:ss'.

S = 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.

Examples

collapse all

Convert Integers to Printable ASCII Character Array

Convert the integers 32–127 into a 3-by-32 array of the printable ASCII characters.

A = (32:127);
S = char(A);
S = reshape(S,32,3)'
S =

 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~

Convert Multiple Arrays to Character Array

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

A1 = [65 66; 67 68];
A2 = 'abcd';
S = char(A1,A2)
S =

AB  
CD  
abcd

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

whos S
  Name      Size            Bytes  Class    Attributes

  S         3x4                24  char               

Convert Cell Array to Character Array

Convert a cell array of character vectors into a character array.

A = {'ABCD','efghijk','','LM'};
S = char(A)
S =

ABCD   
efghijk
       
LM     

Because the character vectors from cell array A have different lengths, char pads S with blanks where needed. char pads the third row with seven blanks, because the third cell of A contains an empty character array.

whos S
  Name      Size            Bytes  Class    Attributes

  S         4x7                56  char               

Convert Duration Array to Character Array

Create a duration array.

D = hours(23:25) + minutes(8) + seconds(1.2345)
D = 

   23.134 hrs   24.134 hrs   25.134 hrs

Convert D to a character array.

S = char(D)
S =

23.134 hrs
24.134 hrs
25.134 hrs

S is a character array with one duration value per row.

Specify the format of the duration values in S.

S = char(D,'hh:mm')
S =

23:08
24:08
25:08

Input Arguments

collapse all

A — Input arraynumeric array | character array | cell array of character vectors | categorical array

Input array, specified as an array of real numbers or characters, a cell array of character vectors, or a categorical array.

If A is a numeric array, then:

  • Nonintegers are rounded towards zero.

  • Values less than 0 are treated as 0.

  • Values greater than 65535 are treated as 65535.

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

D — Input date and timedate or duration array

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

Data Types: datetime | duration | calendarDuration

fmt — Date formatcharacter vector

Date format, specified as a character vector. The supported formats depend on the data type of input 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 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.

locale — Locale of the character vectors to createcharacter vector

Locale of the character vectors to create, specified as a character vector.

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.

These are the same character vectors accepted by the 'Locale' name-value pair argument for the datetime function. The locale affects the language used in the output array.

Example: 'en_US'

Example: 'ja_JP'

Introduced before R2006a

Was this topic helpful?