你好,欢迎访问远方教程PC版!
广告位招租
网站首页 >> 统计之窗 >> MATLAB专区 >> 文章内容

Matlab技巧10:Matlab字符串连接

[日期:2015-10-05]   来源:远方教程  作者:远方教程   阅读:2667次[字体: ] 访问[旧版]
 捐赠远方教程 

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

假定有两个字符串
>> str1='Iloveyou';str2='123';

方法一:

用中括号将str1和str2像矩阵元素一样包含起来:
>> SC=[str1,str2]
 
SC =
 
Iloveyou123
 
(若想验证str1和str2确实被连接起来,可调用length函数测试SC的长度。)

方法二:

用strcat函数
>> SS=strcat(str1,str2)
 
SS =
 
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
 
利用class(STR)得到STR的类型为char。

图片展示
 
相关评论
站长推荐