195

MATLAB 是否具有指示变量类型的函数/运算符(类似于typeofJavaScript 中的运算符)?

4

6 回答 6

234

使用class功能:

>> b = 2
b =
     2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char
于 2009-02-23T17:36:42.130 回答
71

class()函数等价于typeof().

您还可以使用isa()检查变量是否属于特定类型。如果您想更具体,可以使用ischar()isfloat()iscell()等。

于 2009-02-23T17:33:41.933 回答
45

另一个相关的功能是whos。它将列出给定工作区中变量的各种信息(维度、字节大小、类型)。

>> 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 
于 2009-03-11T21:11:27.797 回答
28

使用isa函数时要小心。如果您的对象是指定的类型或其子类之一,这将是正确的。您必须将strcmpclass函数一起使用来测试对象是否专门为该类型而不是子类。

于 2009-03-11T20:57:01.203 回答
5

由于没有人提到它,MATLAB 也有这个metaclass函数,它返回一个对象,其中包含有关传入实体的各种信息。这些meta.class对象可用于继承测试(通过常见的比较运算符)。

例如:

>> metaclass(magic(1))

ans = 

  class with properties:

                     Name: 'double'
              Description: ''
      DetailedDescription: ''
                   Hidden: 0
                   Sealed: 0
                 Abstract: 0
              Enumeration: 0
          ConstructOnLoad: 0
         HandleCompatible: 0
          InferiorClasses: {0×1 cell}
        ContainingPackage: [0×0 meta.package]
     RestrictsSubclassing: 0
             PropertyList: [0×1 meta.property]
               MethodList: [272×1 meta.method]
                EventList: [0×1 meta.event]
    EnumerationMemberList: [0×1 meta.EnumeratedValue]
           SuperclassList: [0×1 meta.class]

>> ?containers.Map <= ?handle

ans =

  logical

   1

我们可以看到class(someObj)相当于Name的结果的字段metaclass(someObj)

于 2017-10-23T10:38:10.223 回答
0

MATLAB - 检查变量的类型

class()与 Javascript 的typeof运算符完全一样。

要获取有关变量的更多详细信息,您可以使用whos命令或whos()函数。

这是在MATLAB R2017a的命令行窗口上执行的示例代码。

>> % 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               

>> 
于 2018-05-10T09:04:19.517 回答