为什么在matlab中,当你输入诸如
之类的语句时 percentage =22
strcat('Transfer is ', num2str(percentage), '% complete');
结果删除了numstr()运算符之前的空格......即
ans = 'Transfer is23% complete'
有没有办法阻止它偷走我的空白?
答案 0 :(得分:3)
这是因为strcat
删除了空格。根据{{1}}:
doc strcat
解决方案:
1)您可以尝试For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form-feed.
2)使用sprintf('Transfer is %d%% complete', percentage);
而不是['Transfer is ', num2str(percentage), '% complete']
进行字符串连接。
答案 1 :(得分:0)
以下内容应该有效:
strcat({'Transfer is '}, num2str(percentage), {'% complete'});
虽然你最终会得到一个单例单元格数组。如果你要连接单个字符串,那么你应该使用[]
而不是strcat
。
就个人而言,我会按照@grapeot的建议使用sprintf
。