Matlab中的字符串和字符有什么区别? [英] What is the difference between strings and characters in 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
内存使用量.字符每个字符正好使用两个字节. Memory use. Chars use exactly two bytes per character. 返回
char
向量,必须使用cell
数组,例如ch = {'a', 'ab', 'abc'}
.使用字符串,可以在实际数组中创建它们:str = [string('a'), string('ab'), string('abc')]
.
但是,要在字符串中索引字符直接使用数组,必须使用大括号表示法:
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
有开销:string
s 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屋!