Main Content

La traduzione di questa pagina non è aggiornata. Fai clic qui per vedere l'ultima versione in inglese.

Conversione del tipo di dati

Convertire tra array numerici, stringhe e array di caratteri, date e orari, array di celle, strutture o tabelle

MATLAB® dispone di molte funzioni per convertire i valori da un tipo di dati a un altro per utilizzarli in contesti diversi. Ad esempio, è possibile convertire i numeri in testo e quindi aggiungerli alle etichette del grafico o ai nomi dei file. I numeri possono essere rappresentati come valori esadecimali o binari. MATLAB fornisce le funzioni per la conversione tra array numerici, stringhe e array di caratteri e array categorical, datetime e duration. È inoltre possibile effettuare conversioni tra i tipi di dati che raggruppano i dati in contenitori, come gli array di celle, le strutture, le tabelle e gli orari. In questi casi, i valori dei dati rimangono gli stessi, ma vengono memorizzati e consultati in un tipo di contenitore diverso.

Funzioni

espandi tutto

Conversione di numeri in testo

stringString array
charCharacter array
cellstrConvert to cell array of character vectors
int2strConvertire i numeri interi in caratteri
mat2strConvert matrix to characters
num2strConvert numbers to character array

Conversione di testo in numeri

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

Conversione dei valori in date e orari

datetimeArrays that represent points in time
durationLengths of time in fixed-length units
matlab.datetime.compatibility.convertDatenumConvert inputs to datetime values in a backward-compatible way (Da R2022a)

Conversione delle date e degli orari in testo

stringString array
charCharacter array
cellstrConvert to cell array of character vectors

Array categorici

categoricalArray that contains values assigned to categories

Tabelle

table2arrayConvertire la tabella in array omogeneo
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

Orari

array2timetableConvert homogeneous array to timetable
table2timetableConvert table to timetable
timetable2tableConvert timetable to table
cell2matConvertire l'array di celle in un array ordinario del tipo di dati sottostante
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

Argomenti

  • 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

    The datetime and duration data types are the recommended data types for working with dates and times. You can convert these data types to text by using the string function, and you can convert text to these data types by using the datetime and duration functions. When you read text representing dates and times from spreadsheets and text files you can automatically convert the text to datetime or duration arrays.

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