>> a = [0 0 7];
>> whos a
Name Size Bytes Class Attributes
a 1x3 24 double
>> b = 'James Bond';
>> whos b
Name Size Bytes Class Attributes
b 1x10 20 char
>> % Define a number
>> num = 67
num =
67
>> % Get type of variable num
>> class(num)
ans =
'double'
>> % Define character vector
>> myName = 'Rishikesh Agrawani'
myName =
'Rishikesh Agrwani'
>> % Check type of myName
>> class(myName)
ans =
'char'
>> % Define a cell array
>> cellArr = {'This ', 'is ', 'a ', 'big chance to learn ', 'MATLAB.'}; % Cell array
>>
>> class(cellArr)
ans =
'cell'
>> % Get more details including type
>> whos num
Name Size Bytes Class Attributes
num 1x1 8 double
>> whos myName
Name Size Bytes Class Attributes
myName 1x17 34 char
>> whos cellArr
Name Size Bytes Class Attributes
cellArr 1x5 634 cell
>> % Another way to use whos i.e using whos(char_vector)
>> whos('cellArr')
Name Size Bytes Class Attributes
cellArr 1x5 634 cell
>> whos('num')
Name Size Bytes Class Attributes
num 1x1 8 double
>> whos('myName')
Name Size Bytes Class Attributes
myName 1x17 34 char
>>
6 回答
class()
函数相当于typeof()
您还可以使用
isa()
来检查变量是否属于特定类型 . 如果您想更具体,可以使用ischar()
,isfloat()
,iscell()
等 .使用
isa
功能时要小心 . 如果您的对象是指定类型或其子类之一,则会出现这种情况 . 您必须使用strcmp
与class
函数来测试对象是否是特定类型而不是子类 .由于没有人提及它,MATLAB还具有metaclass函数,该函数返回一个对象,其中包含有关传入实体的各种信息 . 这些meta.class对象可用于继承测试(通过常见的比较运算符) .
例如:
我们可以看到
class(someObj)
等同于metaclass(someObj)
结果的Name
字段 .使用
class
功能另一个相关的功能是whos . 它将列出给定工作空间中变量的所有类型的信息(维度,字节大小,类型) .
MATLAB - 检查变量类型
这是在 MATLAB R2017a 的命令窗口上执行的示例代码 .