如何在MATLAB中获取变量类型

198

MATLAB有一个函数/运算符可以指示变量的类型吗(类似于JavaScript中的typeof 运算符)?

6个回答

236

使用class函数:

>> b = 2
b =
     2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char

如果条件: if ( string(class(b)) == 'double' ) fprintf(1, 'b is double'); end翻译为中文: - khaverim
请注意,您可以在自定义类中重载“class”方法以返回任意随机字符串。 - Cris Luengo

71

请注意,在自定义类中,您可以重载所有这些方法以返回任何您想要的内容。 - Cris Luengo

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 

1
WHO函数不会列出变量的大小。因此,如果您的工作区很拥挤,它会更快。 - JaBe

28

使用isa函数时要小心,因为它会在对象是指定类型或者其子类时返回true。如果要测试对象是否特定类型而不是其子类,则需要使用class函数的strcmp函数。


这是对Dima的回答的回复吗? - Peter Mortensen

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)等同于 metaclass(someObj)结果的Name字段。

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               

>> 

5
这并没有为目前被接受的答案新增任何内容。 - rayryeng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接