Data Type Conversion
MATLAB® has many functions to convert values from one data type to another for use in different contexts. For example, you can convert numbers to text and then append them to plot labels or file names. You can represent numbers as their hexadecimal or binary values. MATLAB provides functions for conversions between numeric arrays, strings and character arrays, and
duration arrays. Also, you can convert between the data types that group data in containers, such as cell arrays, structures, tables, and timetables. In those cases, the data values remain the same, but they are stored and accessed in a different type of container.
Numbers and Text
Convert Numbers to Text
|Convert to cell array of character vectors|
|Convert integers to characters|
|Convert matrix to characters|
|Convert numbers to character array|
Convert Text to Numbers
Hexadecimal and Binary Numbers
|Convert text representation of base-|
|Convert text representation of binary integer to double value|
|Convert decimal integer to its base-|
|Convert decimal integer to its binary representation|
|Convert decimal integer to its hexadecimal representation|
|Convert text representation of hexadecimal integer to double value|
|Convert IEEE hexadecimal format to double-precision number|
|Convert single- and double-precision numbers to IEEE hexadecimal format|
Dates and Times
Convert Values to Dates and Times
|Arrays that represent points in time|
|Lengths of time in fixed-length units|
|Convert inputs to |
Convert Dates and Times to Text
Categorical Arrays, Tables, and Timetables
|Array that contains values assigned to categories|
|Convert table to homogeneous array|
|Convert table to cell array|
|Convert table to structure array|
|Convert homogeneous array to table|
|Convert cell array to table|
|Convert structure array to table|
Cell Arrays and Structures
- Convert Text to Numeric Values
Convert text to arrays of numeric values. Text can represent floating-point values, dates and times, and hexadecimal and binary numbers. If text represents dates and times, then you can convert the text to datetime or duration values.
- Convert Numeric Values to Text
Convert numeric values to text. Text can represent floating-point values, either with or without exponential notation, or hexadecimal or binary digits. Use these conversions to add numbers to text such as plot labels or titles.
- Unicode and ASCII Values
MATLAB stores all characters as Unicode characters. Both strings and character vectors use the same encoding. You can convert characters to their Unicode code values, and numbers to characters.
- Hexadecimal and Binary Values
Specify hexadecimal and binary values either as literals or as text. Hexadecimal and binary literals are stored as integers. You can convert text representing hexadecimal and binary values to numbers, and numbers to text representations.
- Convert Between Text and datetime or duration Values
durationdata types are the recommended data types for working with dates and times. You can convert these data types to text by using the
stringfunction, and you can convert text to these data types by using the
durationfunctions. When you read text representing dates and times from spreadsheets and text files you can automatically convert the text to
- Convert Text in Table Variables to Categorical
This example shows how to convert a variable in a table from a cell array of character vectors to a categorical array.
- Valid Combinations of Unlike Classes
If you include elements of unlike classes in a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type. Data type conversion is done with respect to a preset precedence of classes.