Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

char

Convert to character array

Syntax

S = char(A)
S = char(A1,...,AN)
S = char(D)
S = char(D,fmt)
S = char(D,fmt,locale)

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

  • Starting in R2016b, if A is a string array, then char converts the string array into a character array. char converts each string element of A into a character vector, and then concatenates the vectors to produce a character array, automatically padded with blank spaces as needed. As a result, S has one more dimension than A.

    To convert S to a string array, use the string function.

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

    To convert S into a cell array of character vectors, 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. The char function pads rows with blank spaces as needed. If any input array is an empty character array, then the corresponding row in S 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

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

Create a string array and convert it to a character array using the char function. Starting in R2017a, you can create strings using double quotes instead of the string function. char pads the rows of the output with blanks as needed.

A = ["Elizabeth";"Jan";"";"Sandra"]
A = 4×1 string array
    "Elizabeth"
    "Jan"
    ""
    "Sandra"

S = char(A)
S = 4×9 char array
    'Elizabeth'
    'Jan      '
    '         '
    'Sandra   '

Text processing functions (such as strfind and regexp) accept string arrays as inputs, but other functions (for example, addpath) do not.

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 = 3×32 char array
    ' !"#$%&'()*+,-./0123456789:;<=>?'
    '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'
    '`abcdefghijklmnopqrstuvwxyz{|}~'

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

whos S
  Name      Size            Bytes  Class    Attributes

  S         3x4                24  char               

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

A = {'ABCD','efghijk','','LM'};
S = char(A)
S = 4×7 char array
    '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               

Create a duration array.

D = hours(23:25) + minutes(8) + seconds(1.2345)
D = 1×3 duration array
   23.134 hr   24.134 hr   25.134 hr

Convert D to a character array.

S = char(D)
S = 3×9 char array
    '23.134 hr'
    '24.134 hr'
    '25.134 hr'

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

Input Arguments

collapse all

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

Starting in R2016b, A can be a string 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.

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

Data Types: datetime | duration | calendarDuration

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 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'

Extended Capabilities

Introduced before R2006a

Was this topic helpful?