【Matlab】连接字符串的方法

来源:互联网 发布:魔术蜘蛛软件2.0 编辑:程序博客网 时间:2024/05/18 16:35

Matlab中,想要将两个字符串连接在一起,有以下的方法:

假定有两个字符串

>>str1='Iloveyou';str2='123';

方法一:用中括号将str1str2像矩阵元素一样包含起来:

>>SC=[str1,str2]

 

SC =

 

Iloveyou123

 

(若想验证str1str2确实被连接起来,可调用length函数测试SC的长度。)

方法二:用strcat函数

>>SB=strcat(str1,str2)

 

SB =

 

Iloveyou123

 

注意strcat函数有许多用法,如下例:

>>strcat({'Red','Yellow'},{'Green','Blue'})

 

ans =

 

   'RedGreen'   'YellowBlue'

 

但下句则结果就不一样了:

>>strcat(['Red','Yellow'],['Green','Blue'])

 

ans =

 

RedYellowGreenBlue

 

方法三:利用sprintf函数

 

>>number=123;

>>STR=sprintf('%s%d',str1,number)

 

STR =

 

Iloveyou123


利用classSTR)得到STR的类型为char


方法四:直接将希望连接的字符串连接起来。

tmp_percent = strcat(tmp2,'_',num2str(100*percent),'%');


tmp_percent =

    'GnomesMARM-OSS-corrected_100%'




 


0 0
原创粉丝点击