Why does STRCAT function ignore trailing white spaces in MATLAB?

20 views (last 30 days)
When I execute the following:
a=10;
strcat('a = ',num2str(a))
I receive the following answer:
ans =
a =10
The space after the equal sign is missing. No matter how many spaces you insert the result is always:
a =10

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to preserve trailing spaces in character array inputs is not available in MATLAB, this is mentioned in the documentation of the STRCAT function:
“Trailing spaces in character array inputs are ignored and do not appear in the output. This is not true for inputs that are cell arrays of strings. Use the concatenation syntax [s1 s2 s3 ...] to preserve trailing spaces.”
To work around this issue, either use :
a=10;
['a = ', num2str(a)]
or
c={' = '};
strcat('a', c, num2str(a))

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!