Flaminator
Flaminator

Reputation: 584

Whitespace elliminated before numstr() in matlab

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?

Upvotes: 2

Views: 285

Answers (2)

Nzbuu
Nzbuu

Reputation: 5251

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.

Upvotes: 0

grapeot
grapeot

Reputation: 1634

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.

Upvotes: 3

Related Questions