字符串与空格的连接

lol*_*ola 5 string matlab concatenation

我想连接字符串.我试过用strcat:

x = 5;
m = strcat('is', num2str(x)) 
Run Code Online (Sandbox Code Playgroud)

但是此函数会从每个字符串中删除尾随空白字符.是否有另一个MATLAB函数来执行字符串连接,以保持尾随空白?

Chr*_*lor 12

您可以使用horzcat而不是strcat:

>> strcat('one ','two')
ans =
onetwo
>> horzcat('one ','two')
ans =
one two
Run Code Online (Sandbox Code Playgroud)

或者,如果您要将数字替换为字符串,则最好使用sprintf:

>> x = 5;
>> sprintf('is %d',x)
ans =
is 5
Run Code Online (Sandbox Code Playgroud)