2

Why is it in matlab, that when you type a statement such as

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

The result removes the whitespace prior to the numstr() operator... i.e

    ans = 'Transfer is23% complete'

Is there a way to prevent it from stealing my whitespace?

Flaminator
  • 564
  • 6
  • 17
  • Duplicate of [Matlab strcat function troubles with spaces](http://stackoverflow.com/questions/1425226/matlab-strcat-function-troubles-with-spaces) – gnovice Jan 25 '12 at 16:19

2 Answers2

3

This is because strcat removes the whitespace. According to doc strcat:

For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form-feed.

Solutions:

1) You may try sprintf('Transfer is %d%% complete', percentage);

2) Use ['Transfer is ', num2str(percentage), '% complete'] rather than strcat for string concatenation.

grapeot
  • 1,594
  • 10
  • 21
0

The following should work:

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

Although you will end up with a singleton cell array. If you're concatenating single strings, then you should really use [] rather than strcat.

Personally, I would use sprintf as suggested by @grapeot.

Nzbuu
  • 5,241
  • 1
  • 29
  • 51