MATLAB 不需要任何類型聲明或維度語(yǔ)句。每當(dāng) MATLAB 遇到一個(gè)新的變量名,它就創(chuàng)建變量并分配適當(dāng)?shù)膬?nèi)存空間。
如果變量已經(jīng)存在,那么MATLAB將用新內(nèi)容替換原始內(nèi)容,并在必要時(shí)分配新的存儲(chǔ)空間。
例如,
Total = 42
上面的語(yǔ)句創(chuàng)建一個(gè)名為“ Total”的1比1矩陣,并將值42存儲(chǔ)在其中。
MATLAB提供了15種基本數(shù)據(jù)類型。每種數(shù)據(jù)類型都存儲(chǔ)矩陣或數(shù)組形式的數(shù)據(jù)。此矩陣或數(shù)組的大小最小為0×0,并且可以增長(zhǎng)到任意大小的矩陣或數(shù)組。
下表顯示了MATLAB中最常用的數(shù)據(jù)類型-
| 序號(hào) | 數(shù)據(jù)類型和說(shuō)明 |
|---|---|
| 1 | int8 8位有符號(hào)整數(shù) |
| 2 | uint8 8位無(wú)符號(hào)整數(shù) |
| 3 | int16 16位有符號(hào)整數(shù) |
| 4 | uint16 16位無(wú)符號(hào)整數(shù) |
| 5 | int32 32位有符號(hào)整數(shù) |
| 6 | uint32 32位無(wú)符號(hào)整數(shù) |
| 7 | int64 64位有符號(hào)整數(shù) |
| 8 | uint64 64位無(wú)符號(hào)整數(shù) |
| 9 | single 單精度數(shù)值數(shù)據(jù) |
| 10 | double 雙精度數(shù)值數(shù)據(jù) |
| 11 | logical 邏輯值1或0,分別表示true和false |
| 12 | char 字符數(shù)據(jù)(字符串存儲(chǔ)為字符向量) |
| 13 | cell array 索引單元格數(shù)組,每個(gè)單元格能夠存儲(chǔ)不同維度和數(shù)據(jù)類型的數(shù)組 |
| 14 | structure 類似于C的結(jié)構(gòu),每個(gè)結(jié)構(gòu)都具有能夠存儲(chǔ)不同維度和數(shù)據(jù)類型的數(shù)組的命名字段 |
| 15 | function handle 指向函數(shù)的指針 |
| 16 | user classes 從用戶定義的類構(gòu)造的對(duì)象 |
| 17 | java classes 從Java類構(gòu)造的對(duì)象 |
使用以下代碼創(chuàng)建腳本文件-
str = 'Hello World!' n = 2345 d = double(n) un = uint32(789.50) rn = 5678.92347 c = int32(rn)
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下結(jié)果-
str = Hello World! n = 2345 d = 2345 un = 790 rn = 5678.9 c = 5679
MATLAB提供了用于將值從一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)的各種函數(shù)。下表顯示了數(shù)據(jù)類型轉(zhuǎn)換函數(shù)-
| 函數(shù) | 作用 |
|---|---|
| char | 轉(zhuǎn)換為字符數(shù)組(字符串) |
| int2str | 將整數(shù)數(shù)據(jù)轉(zhuǎn)換為字符串 |
| mat2str | 將矩陣轉(zhuǎn)換為字符串 |
| num2str | 將數(shù)字轉(zhuǎn)換為字符串 |
| str2double | 將字符串轉(zhuǎn)換為雙精度值 |
| str2num | 將字符串轉(zhuǎn)換為數(shù)字 |
| native2unicode | 將數(shù)字字節(jié)轉(zhuǎn)換為 Unicode 字符 |
| unicode2native | 將Unicode字符轉(zhuǎn)換為數(shù)字字節(jié) |
| base2dec | 將N進(jìn)制數(shù)字符串轉(zhuǎn)換為十進(jìn)制數(shù) |
| bin2dec | 將二進(jìn)制數(shù)字符串轉(zhuǎn)換為十進(jìn)制數(shù) |
| dec2base | 將十進(jìn)制數(shù)轉(zhuǎn)換為字符串中的N進(jìn)制數(shù) |
| dec2bin | 將十進(jìn)制轉(zhuǎn)換為字符串中的二進(jìn)制數(shù) |
| dec2hex | 將字符串中的十進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制數(shù) |
| hex2dec | 將十六進(jìn)制數(shù)字字符串轉(zhuǎn)換為十進(jìn)制數(shù)字 |
| hex2num | 將十六進(jìn)制數(shù)字字符串轉(zhuǎn)換為雙精度數(shù)字 |
| num2hex | 將單精度和雙精度轉(zhuǎn)換為IEEE十六進(jìn)制字符串 |
| cell2mat | 將單元格數(shù)組轉(zhuǎn)換為數(shù)值數(shù)組 |
| cell2struct | 將單元數(shù)組轉(zhuǎn)換為結(jié)構(gòu)數(shù)組 |
| cellstr | 從字符數(shù)組創(chuàng)建字符串的單元格數(shù)組 |
| mat2cell | 將數(shù)組轉(zhuǎn)換為單元格大小可能不同的單元格 |
| num2cell | 將數(shù)組轉(zhuǎn)換為大小一致的單元格數(shù)組 |
| struct2cell | 將結(jié)構(gòu)轉(zhuǎn)換為單元格數(shù)組 |
MATLAB提供了各種功能來(lái)識(shí)別變量的數(shù)據(jù)類型。
下表提供了確定變量的數(shù)據(jù)類型的功能-
| 函數(shù) | 作用 |
|---|---|
| is | 檢測(cè)狀態(tài) |
| isa | 確定輸入是否為指定類的對(duì)象 |
| iscell | 確定輸入是否為單元格數(shù)組 |
| iscellstr | 確定輸入是否為字符串的單元格數(shù)組 |
| ischar | 確定項(xiàng)目是否為字符數(shù)組 |
| isfield | 確定輸入是否為結(jié)構(gòu)數(shù)組字段 |
| isfloat | 確定輸入是否為浮點(diǎn)數(shù)組 |
| ishghandle | 適用于處理圖形對(duì)象句柄 |
| isinteger | 確定輸入是否為整數(shù)數(shù)組 |
| isjava | 確定輸入是否為Java對(duì)象 |
| islogical | 確定輸入是否為邏輯數(shù)組 |
| isnumeric | 確定輸入是否為數(shù)值數(shù)組 |
| isobject | 確定輸入是否為MATLAB對(duì)象 |
| isreal | 檢查輸入是否為實(shí)數(shù)數(shù)組 |
| isscalar | 確定輸入是否為標(biāo)量 |
| isstr | 確定輸入是否為字符數(shù)組 |
| isstruct | 確定輸入是否為結(jié)構(gòu)數(shù)組 |
| isvector | 確定輸入是否為向量 |
| class | 確定對(duì)象的類別 |
| validateattributes | 檢查數(shù)組的有效性 |
| whos | 列出工作空間中的變量,包括大小和類型 |
使用以下代碼創(chuàng)建腳本文件-
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)
運(yùn)行文件時(shí),它將產(chǎn)生以下結(jié)果-
x = 3 ans = 0 ans = 1 ans = 1 ans = 1 ans = 1 x = 23.540 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