MATLAB Answers

strcat including space (i.e, ' ')

2,236 views (last 30 days)
I have to concatenate words, including spaces
Ex. a='word1'; b='word2';c=strcat(a,' ',b);
I need 'word1 word2', however, the value on c is 'word1word2'
Can you help me?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jun 2011
Edited: MathWorks Support Team on 8 Nov 2018
To include spaces when concatenating character vectors, use square brackets.
a = 'word1';
b = 'word2';
c = [a ' ' b]
The “ strcat ” function ignores trailing whitespace characters in character vectors. However, “strcat” preserves them in cell arrays of character vectors or string arrays.
a = {'word1'};
b = {'word2'};
c = strcat(a,{' '},b)
You also can use the “plus” operator to combine strings. Starting in R2017a, use double quotes to create strings. For more information on strings, see the “ string ” data type.
a = "word1";
b = "word2";
c = a + " " + b

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 11 Jun 2011
So? The user also wanted variables instead of constants. I illustrated the principle and the user can adapt from there, such as using
c = char(strcat(a,{' '},b));
Jan
Jan on 11 Jun 2011
@CELL/STRCAT is a not efficiently implemented M-function. It is easy to improve it and remove the strange deleting of marinal spaces. This behaviour is kept from the Matlab 4 times, before CELL-strings allowed to create a container for strings of different lengths.
Paulo Silva
Paulo Silva on 12 Jun 2011
variables, constants, strings and cells
Too many assumptions for such small question but it's all ok

Sign in to comment.

More Answers (2)

Paulo Silva
Paulo Silva on 11 Jun 2011
c=[a ' ' b]
strcat ignores trailing ASCII white space characters and omits all such characters from the output. White space characters in ASCII are space, newline, carriage return, tab, vertical tab, or form-feed characters, all of which return a true response from the MATLAB isspace function. Use the concatenation syntax [s1 s2 s3 ...] to preserve trailing spaces. strcat does not ignore inputs that are cell arrays of strings.

  2 Comments

Daniel Foose
Daniel Foose on 23 Feb 2018
This is better than the accepted answer because it keeps the type the same. The accepted answer returns a cell with a string in it (which is different from a string). This answer returns a string.
Walter Roberson
Walter Roberson on 23 Feb 2018
The accepted answer returns a cell with a character vector in it. Strings did not exist in R2011a. If strings were being used then you would use a different approach:
>> a = "word1"; b = "word2"; a + " " + b
ans =
"word1 word2"
This requires R2017a or later. For R2016b,
>> a = string('word1'); b = string('word2'); a + ' ' + b
and before R2016b strings did not exist.

Sign in to comment.


R P
R P on 11 Jun 2011
Thank you, Walter

  3 Comments

Paulo Silva
Paulo Silva on 11 Jun 2011
Please always test the answers provided before accepting them, Walter answer isn't correct (this time).
Walter Roberson
Walter Roberson on 11 Jun 2011
>> strcat({'word1'},{' '},{'word2'})
ans =
'word1 word2'
You can dereference this or cell2mat it if you want the string itself as output.
Jan
Jan on 11 Jun 2011
@Walter: CELL2MAT is not efficient here. S{1} is nicer.

Sign in to comment.

Sign in to answer this question.

Tags