字符数组
就是字符向量,存储格式为char,用 ' ' 表示;
名称 | 语句 | 存储格式 |
字符数组 | str = 'hello' | 1x5 char |
二维字符数组 | str = char('hello','world') | 2x5 char |
字符向量拼接 | str = ['hello','world'] | 1x10 char |
字符向量拼接 方式2 | str = strcat('hello','world') | 1x10 char |
注意:
str = ['hello','world']为字符向量拼接;
字符串与字符串数组
在MATLAB中用string格式存储,用 “ ” 表示
名称 | 语句 | 存储格式 |
字符串 | str = ‘‘hello’’ | 1x1 string |
字符串数组 | str = ["hello","world"] str = ["hello";"world"] | 1 x 2 string 2 x 1 string |
字符串拼接 | a = "hello" b = "world" c = strcat(a,b); | 1x1 string 1x1 string 1x1 string |
使用cell格式存储字符串
在MATLAB中用cell martrix存储,外面用花括号{}表示;
名称 | 语句 | 存储格式 |
字符串数组 | str = {'hello','world'}; a = str(1); b = str{1}; | 1x2 cell 1x1 cell 1x5 char |
三种格式比较
字符数组与字符串数组(cell格式)间的转换
string:将字符数组转化为字符串,string('hello')
cellstr:cellstr函数将MATLAB字符向量转换为cell格式存储的字符串数组,cellstr('hello')
char:将string格式的字符串或cell格式的字符串转换为char格式的字符数组;