why is a blank ignored in strcat
Show older comments
as an example, the following code:
strcat(num2str(1,'%02.0f'), '-', num2str(2), ' ', num2str(3), ':', num2str(4,'% 2.0f'))
produces:
01-23:4
whereas I want:
01-2 3: 4
Seems pretty simple but ... would someone help please?
Accepted Answer
More Answers (2)
Friedrich
on 15 Oct 2013
Hi,
the doc states:
"For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form-feed. For cell array inputs, strcat does not remove trailing white space."
So you can use:
strcat({num2str(1,'%02.0f')}, {'-'},{num2str(2)}, {' '}, {num2str(3)}, {':'}, {num2str(4,'% 2.0f')})
or dont use strcat and use []:
[num2str(1,'%02.0f'), '-', num2str(2), ' ', num2str(3), ':', num2str(4,'% 2.0f')]
4 Comments
Ross
on 15 Oct 2013
Image Analyst
on 15 Oct 2013
Edited: Jan
on 15 Oct 2013
@Ross: I'm not understanding why you want these crazy complicated ways when the sprintf() Jan suggested is so much more simpler and reliable. I always use sprintf() since I found out what you're finding out now. I suggest you follow Jan's and my recommendations to use sprintf and avoid these problems and simplify your code.
Image Analyst
on 15 Oct 2013
I was talking to Ross. I know Frederich knows both methods and I agree with you about why Frederich answered that way. I was trying to point Ross towards the method that I find gives me much more control with much less complicated syntax.
Jan
on 15 Oct 2013
@Image Analyst: I've inserted "@Ross" in your comment and have removed by concerning question.
Jos (10584)
on 15 Oct 2013
Edited: Jos (10584)
on 15 Oct 2013
This used to be my workaround for the way strcat handles spaces:
strrep(strcat('AAA', '#SPACE#', 'BBB'),'#SPACE#',' ')
2 Comments
Jan
on 15 Oct 2013
What about: ['AAA', ' ', 'BBB'] ?
Jos (10584)
on 15 Oct 2013
I should have stressed the word used ...
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!