Matlab中的字符串和字符有什么区别? [英] What is the difference between strings and characters in Matlab?

查看:1231
本文介绍了Matlab中的字符串和字符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the difference between string and character class in MATLAB?

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

推荐答案

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

There are two ways to represent text in MATLAB®. You can store text in character arrays. A typical use is to store short pieces of text as character vectors. And starting in Release 2016b, you can also store multiple pieces of text in string arrays. String arrays provide a set of functions for working with text as data.

这是两种表示形式的区别:

This is how the two representations differ:

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

  • 内存使用量.字符每个字符正好使用两个字节. string有开销:

  • Memory use. Chars use exactly two bytes per character. strings have overhead:

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

    返回

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

  • 这篇关于Matlab中的字符串和字符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆