Main Content

Data Type Conversion

Convert between numeric arrays, strings and character arrays, dates and times, cell arrays, structures, or tables

MATLAB®有许多函数将值从一个数据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, andcategorical,datetime, anddurationarrays. 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.

Functions

expand all

Convert Numbers to Text

string String array
char Character array
cellstr Convert to cell array of character vectors
int2str Convert integers to characters
mat2str Convert matrix to characters
num2str Convert numbers to character array

Convert Text to Numbers

str2double Convert strings to double precision values
str2num Convert character array or string to numeric array
native2unicode Convert numeric bytes toUnicodecharacter representation
unicode2native ConvertUnicodecharacter representation to numeric bytes
base2dec Convert text representation of base-ninteger to double value
bin2dec Convert text representation of binary integer to double value
dec2base Convert decimal integer to its base-nrepresentation
dec2bin Convert decimal integer to its binary representation
dec2hex Convert decimal integer to its hexadecimal representation
hex2dec Convert text representation of hexadecimal integer to double value
hex2num ConvertIEEEhexadecimal format to double-precision number
num2hex Convert single- and double-precision numbers toIEEEhexadecimal format

Convert Values to Dates and Times

datetime Arrays that represent points in time
duration Lengths of time in fixed-length units

Convert Dates and Times to Text

string String array
char Character array
cellstr Convert to cell array of character vectors

Categorical Arrays

categorical Array that contains values assigned to categories

Tables

table2array Convert table to homogeneous array
table2cell Convert table to cell array
table2struct Convert table to structure array
array2table Convert homogeneous array to table
cell2table Convert cell array to table
struct2table Convert structure array to table

Timetables

array2timetable Convert homogeneous array to timetable
table2timetable Convert table to timetable
timetable2table Convert timetable to table
cell2mat Convert cell array to ordinary array of the underlying data type
cell2struct Convert cell array to structure array
mat2cell Convert array to cell array whose cells contain subarrays
num2cell Convert array to cell array with consistently sized cells
struct2cell Convert structure to cell array

Topics

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 Datetime Arrays, Numbers, and Text

Whiledatetimeis the recommended data type for representing points in time, you might need to work with dates and time stored as date strings, date vectors, and serial date numbers.

Convert Text in Table Variables to Categorical

这个例子展示了如何将一个变量中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.