matlab中字符串分割以及将⼯作区的变量写⼊txt⽂本中
⼀字符串分割
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
matlab二进制字符串转数组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,';')

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。