【问题标题】:Whitespace elliminated before numstr() in matlabmatlab中numstr()之前的空格被消除
【发布时间】:2012-01-25 05:19:25
【问题描述】:

为什么在matlab中,当你输入一个语句比如

    percentage =22
    strcat('Transfer is ', num2str(percentage), '% complete');

结果删除了 numstr() 运算符之前的空格...即

    ans = 'Transfer is23% complete'

有没有办法防止它窃取我的空格?

【问题讨论】:

标签: matlab


【解决方案1】:

这是因为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 进行字符串连接。

【讨论】:

    【解决方案2】:

    以下应该有效:

    strcat({'Transfer is '}, num2str(percentage), {'% complete'});
    

    虽然您最终会得到一个单例元胞数组。如果你要连接单个字符串,那么你真的应该使用[] 而不是strcat

    就个人而言,我会按照@grapeot 的建议使用sprintf

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 2017-02-03
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 2018-03-21
      • 2013-12-01
      相关资源
      最近更新 更多