Main Content

La traducción de esta página está obsoleta. Haga clic aquí para ver la última versión en inglés.

Conversión de tipos de datos

Convierta entre arreglos numéricos, cadenas y arreglos de caracteres, fechas y horas, arreglos de celdas, estructuras o tablas

MATLAB® tiene muchas funciones para convertir valores de un tipo de datos a otro para su uso en diferentes contextos. Por ejemplo, puede convertir números en texto y, a continuación, añadirlos a etiquetas de gráficas o nombres de archivos. Puede representar números como sus valores binarios o hexadecimales. MATLAB proporciona funciones para las conversiones entre arreglos numéricos, cadenas y arreglos de caracteres y arreglos categorical, datetime y duration. También puede convertir entre los tipos de datos que agrupan datos en contenedores, como los arreglos de celdas, estructuras, tablas y horarios. En esos casos, los valores de datos permanecen igual, pero se almacenan y se accede a ellos en un tipo diferente de contenedor.

Funciones

expandir todo

Convertir números a texto

stringString array
charCharacter array
cellstrConvert to cell array of character vectors
int2strConvert integers to characters
mat2strConvert matrix to characters
num2strConvert numbers to character array

Convertir texto a números

str2doubleConvert strings to double precision values
str2numConvert character array or string to numeric array
native2unicodeConvert numeric bytes to Unicode character representation
unicode2nativeConvert Unicode character representation to numeric bytes
base2decConvert text representation of base-n integer to double value
bin2decConvert text representation of binary integer to double value
dec2baseConvert decimal integer to its base-n representation
dec2binConvert decimal integer to its binary representation
dec2hexConvert decimal integer to its hexadecimal representation
hex2decConvert text representation of hexadecimal integer to double value
hex2numConvert IEEE hexadecimal format to double-precision number
num2hexConvert single- and double-precision numbers to IEEE hexadecimal format

Convertir valores a fechas y horas

datetimeArrays that represent points in time
durationLengths of time in fixed-length units

Convertir fechas y horas a texto

stringString array
charCharacter array
cellstrConvert to cell array of character vectors

Arreglos categóricos

categoricalArray that contains values assigned to categories

Tablas

table2arrayConvert table to homogeneous array
table2cellConvert table to cell array
table2structConvert table to structure array
array2tableConvert homogeneous array to table
cell2tableConvert cell array to table
struct2tableConvert structure array to table

Horarios

array2timetableConvert homogeneous array to timetable
table2timetableConvert table to timetable
timetable2tableConvert timetable to table
cell2matConvert cell array to ordinary array of the underlying data type
cell2structConvert cell array to structure array
mat2cellConvert array to cell array whose cells contain subarrays
num2cellConvert array to cell array with consistently sized cells
struct2cellConvert structure to cell array

Temas

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

While datetime is 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

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.