问题描述
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.
推荐答案
文档建议:
这是两种表示形式的区别:
This is how the two representations differ:
-
元素访问权限.为了表示不同长度的
char
向量,必须使用cell
数组,例如ch = {'a', 'ab', 'abc'}
.使用字符串,可以在实际数组中创建它们:str = [string('a'), string('ab'), string('abc')]
.但是,要在字符串中索引字符直接使用数组,必须使用大括号表示法:
Element access. To represent
char
vectors of different length, one had to usecell
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. 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中的字符串和字符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!