Why does the STRCAT command remove the trailing spaces while performing a concatenation in MATLAB 7.4 (R2007a)?

8 views (last 30 days)
When I was performing some string concatenations, I noticed that STRCAT removes some characters that are not empty spaces, including new line characters from my strings.
I added a new line after a hello in string a. I did this using the SPRINTF command.
a = sprintf('hello \n')
b = 'world'
c = strcat (a , b)
For the above piece of code I received the following result
c = helloworld
I added defind two strings as shown below and I have added 5 spaces after hello in string a.
a = 'hello '
b = ' world'
c = strcat (a , b)
I received the following result
c = helloworld
Why the newline is also recognized as a trailing whitespace character?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This enhancement has been incorporated in Release 2008b (R2008b). For previous product releases, read below for any possible workarounds:
In MATLAB 7.4 (R2007a), the STRCAT command uses the ISSPACE function on trailing characters. If the result is true then STRCAT command ignores that space while performing concatenation. The characters which return a true value are ASCII white spaces including: space, newline, carriage return, tab, vertical tab and formfeed characters.
As a workaround, you can use the SPRINTF function to concatenate two strings. This function requires that you use formatting to specify what type of input you are using, but it will not remove any characters. For example, to concatenate a string with a new line character, you would use the following code:
a = sprintf('%s \n','Hello ');
b = 'world';
c = sprintf('%s %s',a,b)
This will result in:
c =
Hello
world

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2007a

Community Treasure Hunt

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

Start Hunting!