一 字符串分割
matlab中最常用的字符串分割函数有两个,都比较好用,分别是strsplit和strtok。
1 strsplit函数
假设需要分割的字符串为str,直接使用 strsplit(str) 就可以分割,默认按空白字符分割,分割后的字符组成元胞数组。
>> str = \'hello world, I am a student!\'
str =
hello world, I am a student!
>> s = strsplit(str);
>> s
s =
1×6 cell 数组
\'hello\' \'world,\' \'I\' \'am\' \'a\' \'student!\'
>> s{1,1}
ans =
hello
>> s{1,2}
ans =
world,
>>
strsplit的第二个参数可以是分割字符,比如用\',\'或者\'.\'或者\'-\'等进行字符串的分割,第二个参数甚至可以是包含多个分割字符的元胞数组,如下
>> str = \'With,the,development,of,society.people-have-higher-requirements-for-image-quality\'
str =
With,the,development,of,society.people-have-higher-requirements-for-image-quality
>> s1 = strsplit(str,\',\')
s1 =
1×5 cell 数组
\'With\' \'the\' \'development\' \'of\' \'society.people-have-…\'
>> s1{1,5}
ans =
society.people-have-higher-requirements-for-image-quality
>> s2 = strsplit(str,\'-\')
s2 =
1×7 cell 数组
\'With,the,developm…\' \'have\' \'higher\' \'requirements\' \'for\' \'image\' \'quality\'
>> s3 = strsplit(str,\'.\')
s3 =
1×2 cell 数组
\'With,the,development,of,society\' \'people-have-higher-requirements-for-image-quality\'
>> s4 = strsplit(str,{\',\',\'.\',\'-\'})
s4 =
1×12 cell 数组
1 至 11 列
\'With\' \'the\' \'development\' \'of\' \'society\' \'people\' \'have\' \'higher\' \'requirements\' \'for\' \'image\'
12 列
\'quality\'
>>
strsplit=最多可以有两个返回值,第二个返回值是匹配到的分割字符。
[s1,s2] = strsplit(str,\'.\')
s1 =
1×2 cell 数组
\'With,the,development,of,society\' \'people-have-higher-requirements-for-image-quality\'
s2 =
cell
\'.\'
>> [s1,s2] = strsplit(str,\',\')
s1 =
1×5 cell 数组
\'With\' \'the\' \'development\' \'of\' \'society.people-have…\'
s2 =
1×4 cell 数组
\',\' \',\' \',\' \',\'
>> [s1,s2,s3] = strsplit(str,\',\')
错误使用 strsplit
输出参数太多。
strsplit还可以有参数\'DelimiterType\',当值为\'RegularExpression\'时,将分隔字符串按照正则表达式理解。
>> str = \'ab1cd2ef3gh4ij5kl6mn7.\'
str =
ab1cd2ef3gh4ij5kl6mn7.
>> s = strsplit(str,\'[0-9]\',\'DelimiterType\',\'RegularExpression\')
s =
1×8 cell 数组
\'ab\' \'cd\' \'ef\' \'gh\' \'ij\' \'kl\' \'mn\' \'.\'
>> [s1,s2] = strsplit(str,\'[0-9]\',\'DelimiterType\',\'RegularExpression\')
s1 =
1×8 cell 数组
\'ab\' \'cd\' \'ef\' \'gh\' \'ij\' \'kl\' \'mn\' \'.\'
s2 =
1×7 cell 数组
\'1\' \'2\' \'3\' \'4\' \'5\' \'6\' \'7\'
>>
2 strtok函数
strtok一般只分成两部分,默认会在从头开始遇到的第一个空格/tab/换行符处断开,也可以指定分割字符。
>> str = \'hello world, i am a student\' str = hello world, i am a student >> s1 = strtok(str) s1 = hello >> s2 = strtok(str,\',\') s2 = hello world
strtok可以有两个返回值,第一个是分割后的前一部分,第二个是分割后的剩余部分(包括分割字符)。
>> [s3 s4] = strtok(str) s3 = hello s4 = world, i am a student >> [s3 s4] = strtok(str,\',\') s3 = hello world s4 = , i am a student
strtok的输入也可以是元胞数组,返回的两个返回值也是对应的元胞数组。
>> str = {\'hello world\';\'good job\'}
str =
2×1 cell 数组
\'hello world\'
\'good job\'
>> [s1 s2] = strtok(str)
s1 =
2×1 cell 数组
\'hello\'
\'good\'
s2 =
2×1 cell 数组
\' world\'
\' job\'
>>
二 将工作区的变量写入txt文本中
这里指的是将mat中的矩阵按一定格式存入txt文本中,在数据处理中经常用到,直接粘贴复制的话比较麻烦而且未必满足格式,比如我有一个名为cov_prisparam的mat文件,是一个36*36的double矩阵。如下
我想要将其写入txt文件中,并且用逗号分割。如果直接用save存的话会出现乱码,save(\'li.txt\',\'cov_prisparam\')
查了一下才知道要加参数ascii,这样就可以了,save(\'li.txt\',\'cov_prisparam\',\'-ascii\')
但是他的格式都被统一成科学计数法了,而且是文本形式,这样显然不方便数据的调用,后来查到一个非常好用的函数dlmwrite,dlmwrite(\'li.txt\',cov_prisparam),默认分割符是逗号。
也可以指定其他分隔符,比如分号或者空格等,dlmwrite(\'li.txt\',cov_prisparam,\';\')