📜  MATLAB-数据类型

📅  最后修改于: 2020-11-03 09:45:21             🧑  作者: Mango


MATLAB不需要任何类型声明或尺寸声明。每当MATLAB遇到新的变量名称时,它都会创建变量并分配适当的内存空间。

如果变量已经存在,则MATLAB会用新内容替换原始内容,并在必要时分配新的存储空间。

例如,

Total = 42

上面的语句创建一个名为“ Total”的1比1矩阵,并将值42存储在其中。

MATLAB中可用的数据类型

MATLAB提供了15种基本数据类型。每种数据类型都存储矩阵或数组形式的数据。此矩阵或数组的大小最小为0×0,并且可以增长到任意大小的矩阵或数组。

下表显示了MATLAB中最常用的数据类型-

Sr.No. Data Type & Description
1

int8

8-bit signed integer

2

uint8

8-bit unsigned integer

3

int16

16-bit signed integer

4

uint16

16-bit unsigned integer

5

int32

32-bit signed integer

6

uint32

32-bit unsigned integer

7

int64

64-bit signed integer

8

uint64

64-bit unsigned integer

9

single

single precision numerical data

10

double

double precision numerical data

11

logical

logical values of 1 or 0, represent true and false respectively

12

char

character data (strings are stored as vector of characters)

13

cell array

array of indexed cells, each capable of storing an array of a different dimension and data type

14

structure

C-like structures, each structure having named fields capable of storing an array of a different dimension and data type

15

function handle

pointer to a function

16

user classes

objects constructed from a user-defined class

17

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 = 5678.9
c =  5679

数据类型转换

MATLAB提供了用于将值从一种数据类型转换为另一种数据的各种功能。下表显示了数据类型转换函数-

Function Purpose
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提供了多种功能来识别变量的数据类型。

下表提供了确定变量的数据类型的功能-

Function Purpose
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-point 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.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