【发布时间】:2012-01-25 05:19:25
【问题描述】:
为什么在matlab中,当你输入一个语句比如
percentage =22
strcat('Transfer is ', num2str(percentage), '% complete');
结果删除了 numstr() 运算符之前的空格...即
ans = 'Transfer is23% complete'
有没有办法防止它窃取我的空格?
【问题讨论】:
标签: matlab
为什么在matlab中,当你输入一个语句比如
percentage =22
strcat('Transfer is ', num2str(percentage), '% complete');
结果删除了 numstr() 运算符之前的空格...即
ans = 'Transfer is23% complete'
有没有办法防止它窃取我的空格?
【问题讨论】:
标签: matlab
这是因为strcat 删除了空格。根据doc strcat:
For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form-feed.
解决方案:
1) 你可以试试sprintf('Transfer is %d%% complete', percentage);
2) 使用['Transfer is ', num2str(percentage), '% complete'] 而不是strcat 进行字符串连接。
【讨论】:
以下应该有效:
strcat({'Transfer is '}, num2str(percentage), {'% complete'});
虽然您最终会得到一个单例元胞数组。如果你要连接单个字符串,那么你真的应该使用[] 而不是strcat。
就个人而言,我会按照@grapeot 的建议使用sprintf。
【讨论】: