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

  • 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

Starting in R2016b, you can create string arrays using the string function. You can convert a string array to a character array using the char function. The character array has a different size because char converts each string to a row of characters in the character array. You also can convert a character array back to a string array with the same size as the original string array.

Create a 4-by-1 string array.

A = string({'Elizabeth';'John';'';'Stella'})
A = 

  4×1 string array

    "Elizabeth"
    "John"
    ""
    "Stella"

Convert the string array to a 4-by-9 character array. char pads the rows that have fewer than nine characters with blanks.

S = char(A)
S =

Elizabeth
John     
         
Stella   

Create a 1-by-4 string array.

A = string({'Elizabeth','John','','Stella'})
A = 

  1×4 string array

    "Elizabeth"    "John"    ""    "Stella"

Convert the string array. char converts A to a 1-by-9-by-4 character array.

S = char(A)
S(:,:,1) =

Elizabeth

S(:,:,2) =

John     

S(:,:,3) =

         

S(:,:,4) =

Stella   

Convert S back into a string array. A2 is also a 1-by-4 string array.

A2 = string(S)
A2 = 

  1×4 string array

    "Elizabeth"    "John     "    "         "    "Stella   "

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 =

 !"#$%&'()*+,-./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 =

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 =

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 =

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 =

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'

More About

collapse all

Tall Array Support

This function supports tall arrays with the limitations:

  • For the syntax S = char(X), the input X must be a tall numeric column vector.

  • Syntaxes with more than one input are not supported.

For more information, see Tall Arrays.

Introduced before R2006a

Was this topic helpful?