Convert to character array (string)




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, with 0 to 127 corresponding to 7-bit ASCII characters. Values from 128 to 65535 produce characters that depend upon your locale setting.

  • 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 strings, then char converts the cell array into a character array. The strings 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.


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


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.


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 strings such as month and day names.


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 = reshape(32:127,32,3)';
S = char(A)
S =


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 =


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 of Strings to Character Array

Convert a cell array of strings into a character array.

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


Because the input strings from cell array A have different lengths, char pads the strings with blanks. char pads the third row with seven blanks, because the third cell of A contains an empty string.

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 =


Input Arguments

collapse all

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

Input array, specified as an array of real numbers or characters, a cell array of strings, 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 formatstring

Date format, specified as a string. 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 strings to createstring

Locale of the character strings to create, specified as a string.

locale can be:

  • 'system', to specify your system locale.

  • a string 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 strings 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?