String 带空格的字符串连接
我想连接字符串。我尝试使用strcatString 带空格的字符串连接,string,matlab,concatenation,String,Matlab,Concatenation,我想连接字符串。我尝试使用strcat x = 5; m = strcat('is', num2str(x)) >> strcat('one ','two') ans = onetwo >> horzcat('one ','two') ans = one two m = ['is ', num2str(x)] 但此函数从每个字符串中删除尾随的空白字符。是否有另一个MATLAB函数来执行字符串连接以保持尾部空白?您可以使用horzcat而不是strcat: x =
x = 5;
m = strcat('is', num2str(x))
>> strcat('one ','two')
ans =
onetwo
>> horzcat('one ','two')
ans =
one two
m = ['is ', num2str(x)]
但此函数从每个字符串中删除尾随的空白字符。是否有另一个MATLAB函数来执行字符串连接以保持尾部空白?您可以使用horzcat
而不是strcat
:
x = 5;
m = strcat('is', num2str(x))
>> strcat('one ','two')
ans =
onetwo
>> horzcat('one ','two')
ans =
one two
m = ['is ', num2str(x)]
或者,如果要将数字替换为字符串,最好使用sprintf
:
>> x = 5;
>> sprintf('is %d',x)
ans =
is 5
这不考虑哪些空间?只有你没有提到的空间!你是说:
m = strcat( ' is ',num2str(x) )
也许吧
Matlab不会猜测(a)您想要空间,或者(b)将其猜测的空间放置在何处。请看最后一个示例:尝试使用水平数组concatation而不是strcat
:
x = 5;
m = strcat('is', num2str(x))
>> strcat('one ','two')
ans =
onetwo
>> horzcat('one ','two')
ans =
one two
m = ['is ', num2str(x)]
此外,还可以查看有关字符串格式(前导/尾随空格等)的更多信息。如何
strcat({' is '},{num2str(5)})
那就
' is 5'
使用如何
如果你使用strcat,你放在那里的空间会被删掉