Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 带空格的字符串连接_String_Matlab_Concatenation - Fatal编程技术网

String 带空格的字符串连接

String 带空格的字符串连接,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 =

我想连接字符串。我尝试使用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 = 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,你放在那里的空间会被删掉