【问题标题】:What is the difference between strings and characters in Matlab?Matlab中的字符串和字符有什么区别?
【发布时间】:2017-06-26 11:05:33
【问题描述】:

MATLAB中字符串类和字符类有什么区别?

a = 'AX'; % This is a character.
b = string(a) % This is a string.

【问题讨论】:

    标签: string matlab char


    【解决方案1】:

    documentation suggests:

    在 MATLAB® 中有两种表示文本的方法。您可以将文本存储在字符数组中。一个典型的用途是将短文本存储为字符向量。从 2016b 版开始,您还可以将多段文本存储在字符串数组中。字符串数组提供了一组将文本作为数据处理的函数。

    这就是这两种表示的不同之处:

    • 元素访问。要表示不同长度的char 向量,必须使用cell 数组,例如ch = {'a', 'ab', 'abc'}。使用字符串,可以在实际数组中创建它们:str = [string('a'), string('ab'), string('abc')]。 但是,要直接在字符串数组中使用index characters,则必须使用大括号表示法:

      str{3}(2) % == 'b'
      
    • 内存使用。 Chars 每个字符恰好使用两个字节。 strings 有开销:

      a = 'abc'
      b = string('abc')
      whos a b
      

      返回

      Name      Size            Bytes  Class     Attributes
      
       a         1x3                 6  char                
       b         1x1               132  string
      

    【讨论】:

      【解决方案2】:

      了解差异的最佳起点是the documentation。关键区别,如其中所述:

      • 字符数组是字符序列,就像数值数组是数字序列一样。一个典型的用途是将短文本存储为字符向量,例如c = 'Hello World';
      • 字符串数组是文本片段的容器。字符串数组提供了一组将文本作为数据处理的函数。要将文本转换为字符串数组,请使用 string 函数。

      以下是关于它们差异的更多关键点:

      • 它们是不同的类(即类型):charstring。因此,它们将为每个定义不同的方法集。考虑一下您想对文本执行哪种操作,然后选择最能支持这些操作的操作。
      • 由于string 是一个容器类,请注意它的大小与等效字符数组表示的不同之处。使用您的示例:

        >> a = 'AX'; % This is a character.
        >> b = string(a) % This is a string.
        >> whos
          Name      Size            Bytes  Class     Attributes
        
          a         1x2                 4  char                
          b         1x1               134  string
        

        注意string 容器将其大小列为1x1(并在内存中占用更多字节),而字符数组顾名思义是1x2 字符数组。

      • 它们不能始终互换使用,您可能需要在两者之间进行转换以进行某些操作。比如string对象不能作为dynamic field names for structure indexing

        >> s = struct('a', 1);
        >> name = string('a');
        >> s.(name)
        Argument to dynamic structure reference must evaluate to a valid field name.
        
        >> s.(char(name))
        
        ans =
        
             1
        

      【讨论】:

      【解决方案3】:

      字符串确实有一点开销,但每个字符仍会增加 2 个字节。每 8 个字符后,它会增加变量的大小。红线是y=2x+127

      图是使用以下方法创建的:

      v=[];N=100;
      for ct = 1:N
          s=char(randi([0 255],[1,ct]));
          s=string(s);
          a=whos('s');v(ct)=a.bytes;
      end
      figure(1);clf
      plot(v)
      xlabel('# characters')
      ylabel('# bytes')
      p=polyfit(1:N,v,1);
      hold on
      plot([0,N],[127,2*N+127],'r')
      hold off
      

      【讨论】:

        【解决方案4】:

        需要注意的一件重要的实际事情是,字符串和字符在与方括号交互时表现不同。当来自 python 时,这可能会特别令人困惑。考虑以下示例:

        >>['asdf' '123']
        
        ans =
        
            'asdf123'
        
        >> ["asdf" "123"]
        
        ans = 
        
          1×2 string array
        
            "asdf"    "123"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-17
          • 1970-01-01
          • 1970-01-01
          • 2011-02-26
          • 1970-01-01
          • 2011-11-13
          相关资源
          最近更新 更多