MATLAB數據類型
MATLAB 並不需要任何類型的聲明或維度報表。 MATLAB 每當遇到一個新的變量名稱,創建變量,並分配適當的內存空間。
如果變量已經存在,則MATLAB替換以新的內容的原始內容,並分配新的存儲空間,在必要的情況下。
例如,
Total = 42
上述語句創建了一個名為“Total” 的 1-1 矩陣存儲值42。
MATLAB中可用的數據類型
MATLAB 提供15個基本數據類型。每種數據類型的數據存儲在矩陣或陣列的形式。這個矩陣的大小或陣列是一個最低 0-0,這可以長大為任何規模大小的矩陣或數組。
下表顯示了在 MATLAB 中最常用的數據類型:
數據類型 | 描述 |
---|---|
int8 | 8-bit signed integer |
uint8 | 8-bit unsigned integer |
int16 | 16-bit signed integer |
uint16 | 16-bit unsigned integer |
int32 | 32-bit signed integer |
uint32 | 32-bit unsigned integer |
int64 | 64-bit signed integer |
uint64 | 64-bit unsigned integer |
single | single precision numerical data |
double | double precision numerical data |
logical | logical values of 1 or 0, represent true and false respectively |
char | character data (strings are stored as vector of characters) |
cell array | array of indexed cells, each capable of storing an array of a different dimension and data type |
structure | C-like structures, each structure having named fields capable of storing an array of a different dimension and data type |
function handle | yiibaier to a function |
user classes | objects constructed from a user-defined class |
java classes | objects constructed from a Java class |
例子
創建一個腳本文件,用下麵的代碼:
str = 'Hello World!' n = 2345 d = double(n) un = uint32(789.50) rn = 5678.92347 c = int32(rn)
上麵的代碼編譯和執行時,它會產生以下結果:
str = Hello World! n = 2345 d = 2345 un = 790 rn = 5.6789e+03 c = 5679
數據類型轉換
MATLAB 提供各種函數,用於從一種數據類型轉換到另一種。下表顯示的數據類型轉換函數:
函數 | 目的/作用 |
---|---|
char | Convert to character array (string) |
int2str | Convert integer data to string |
mat2str | Convert matrix to string |
num2str | Convert number to string |
str2double | Convert string to double-precision value |
str2num | Convert string to number |
native2unicode | Convert numeric bytes to Unicode characters |
unicode2native | Convert Unicode characters to numeric bytes |
base2dec | Convert base N number string to decimal number |
bin2dec | Convert binary number string to decimal number |
dec2base | Convert decimal to base N number in string |
dec2bin | Convert decimal to binary number in string |
dec2hex | Convert decimal to hexadecimal number in string |
hex2dec | Convert hexadecimal number string to decimal number |
hex2num | Convert hexadecimal number string to double-precision number |
num2hex | Convert singles and doubles to IEEE hexadecimal strings |
cell2mat | Convert cell array to numeric array |
cell2struct | Convert cell array to structure array |
cellstr | Create cell array of strings from character array |
mat2cell | Convert array to cell array with potentially different sized cells |
num2cell | Convert array to cell array with consistently sized cells |
struct2cell | Convert structure to cell array |
測定的數據類型
MATLAB 提供各種函數標識數據類型的變量。
下表提供了確定一個變量的數據類型的函數:
函數 | 目的/作用 |
---|---|
is | Detect state |
isa | Determine if input is object of specified class |
iscell | Determine whether input is cell array |
iscellstr | Determine whether input is cell array of strings |
ischar | Determine whether item is character array |
isfield | Determine whether input is structure array field |
isfloat | Determine if input is floating-yiibai array |
ishghandle | True for Handle Graphics object handles |
isinteger | Determine if input is integer array |
isjava | Determine if input is Java object |
islogical | Determine if input is logical array |
isnumeric | Determine if input is numeric array |
isobject | Determine if input is MATLAB object |
isreal | Check if input is real array |
isscalar | Determine whether input is scalar |
isstr | Determine whether input is character array |
isstruct | Determine whether input is structure array |
isvector | Determine whether input is vector |
class | Determine class of object |
validateattributes | Check validity of array |
whos | List variables in workspace, with sizes and types |
例子
創建一個腳本文件,用下麵的代碼:
x = 3 isinteger(x) isfloat(x) isvector(x) isscalar(x) isnumeric(x) x = 23.54 isinteger(x) isfloat(x) isvector(x) isscalar(x) isnumeric(x) x = [1 2 3] isinteger(x) isfloat(x) isvector(x) isscalar(x) x = 'Hello' isinteger(x) isfloat(x) isvector(x) isscalar(x) isnumeric(x)
當運行該文件,它會產生以下結果:
x = 3 ans = 0 ans = 1 ans = 1 ans = 1 ans = 1 x = 23.5400 ans = 0 ans = 1 ans = 1 ans = 1 ans = 1 x = 1 2 3 ans = 0 ans = 1 ans = 1 ans = 0 x = Hello ans = 0 ans = 0 ans = 1 ans = 0 ans = 0