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

 

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,';')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM