Matlab中的字符串和字符有什么区别?

17

在MATLAB中,字符串和字符类之间有什么区别?

a = 'AX'; % This is a character.
b = string(a) % This is a string.
4个回答

15

文档建议:

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

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

  • Element access. To represent char vectors of different length, one had to use cell arrays, e.g. ch = {'a', 'ab', 'abc'}. With strings, they can be created in actual arrays: str = [string('a'), string('ab'), string('abc')]. However, to index characters in a string array directly, the curly bracket notation has to be used:

    str{3}(2) % == 'b'
    
  • Memory use. Chars use exactly two bytes per character. strings have overhead:

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

    returns

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

6

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

>>['asdf' '123']

ans =

    'asdf123'

>> ["asdf" "123"]

ans = 

  1×2 string array

    "asdf"    "123"

5

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

string class

图形是使用以下命令创建的:

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

5
理解两者差异的最佳起点是文档。如文档所述,关键区别在于:
  • 字符数组是一系列字符,就像数字数组是一系列数字一样。典型用法是将短文本存储为字符向量,例如 c = 'Hello World';
  • 字符串数组是文本片段的容器。字符串数组提供了一组函数来处理文本作为数据。要将文本转换为字符串数组,请使用string函数。
以下是它们之间的更多关键区别:
  • They are different classes (i.e. types): char versus string. As such they will have different sets of methods defined for each. Think about what sort of operations you want to do on your text, then choose the one that best supports those.
  • Since a string is a container class, be mindful of how its size differs from an equivalent character array representation. Using your example:

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

    Notice that the string container lists its size as 1x1 (and takes up more bytes in memory) while the character array is, as its name implies, a 1x2 array of characters.

  • They can't always be used interchangeably, and you may need to convert between the two for certain operations. For example, string objects can't be used as 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
    

似乎字符串现在可以用作结构索引的动态字段名称(截至Matlab 2017b):https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html - Adam L. Taylor

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