Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab strcat function troubles with spaces

Tags:

matlab

strcat

I'm trying to accomplish this:

strcat('red ', 'yellow ', 'white ')

I expected to see "red yellow white", however, I see "redyellowwhite" on the command output. What needs to be done to ensure the spaces are concatenated properly? Thanks in advance.

like image 995
stanigator Avatar asked Sep 15 '09 05:09

stanigator


People also ask

How do you put spaces in strcat?

Add a whitespace at the end of str1 before concatenation with str2 , or add a whitespace at the beginning of str2 and then concatenate the two strings... Show activity on this post. size_t Len = strlen(str1); strcpy_s(&str1[Len], Len - sizeof(str1), " ") || strcpy_s(&str1[Len+1], Len - (sizeof(str1) + 1), str2);

How do I get rid of extra spaces in MATLAB?

Description. newStr = strtrim( str ) removes leading and trailing whitespace characters from str and returns the result as newStr .

How does strcat work in MATLAB?

Description. s = strcat( s1,...,sN ) horizontally concatenates the text in its input arguments. Each input argument can be a character array, a cell array of character vectors, or a string array. If any input is a string array, then the result is a string array.


5 Answers

From the matlab help page for strcat:

"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. "

like image 191
nsanders Avatar answered Oct 04 '22 16:10

nsanders


Although STRCAT ignores trailing white space, it still preserves leading white space. Try this:

strcat('red',' yellow',' white')

Alternatively, you can just use the concatenation syntax:

['red ' 'yellow ' 'white ']
like image 41
gnovice Avatar answered Oct 04 '22 16:10

gnovice


In fact, you can simply use the ASCII code of space: 32. So, you can solve the problem like this:

str = strcat('red', 32, 'yellow', 32, 'white');

Then you will get str = 'red yellow white'.

like image 43
Kevin_Hu Avatar answered Oct 04 '22 16:10

Kevin_Hu


You can protect trailing whitespace in strcat() or similar functions by putting it in a cell.

str = strcat({'red '}, {'yellow '}, {'white '})
str = str{1}

Not very useful in this basic example. But if you end up doing "vectorized" operations on the strings, it's handy. Regular array concatenation doesn't do the 1-to-many concatenation that strcat does.

strs = strcat( {'my '}, {'red ','yellow ','white '}, 'shirt' )

Sticking 'my ' in a cell even though it's a single string will preserve the whitespace. Note you have to use the {} form instead of calling cellstr(), which will itself strip trailing whitespace.

This is all probably because Matlab has two forms of representing lists of strings: as a cellstr array, where all whitespace is significant, and as a blank-padded 2-dimensional char array, with each row treated as a string, and trailing whitespace ignored. The cellstr form most resembles strings in Java and C; the 2-D char form can be more memory efficient if you have many strings of similar length. Matlab's string manipulation functions are polymorphic on the two representations, and sometimes exhibit differences like this. A char literal like 'foo' is a degenerate one-string case of the 2-D char form, and Matlab's functions treat it as such.

UPDATE: As of Matlab R2019b or so, Matlab also has a new string array type. Double-quoted string literals make string arrays instead of char arrays. If you switch to using string arrays, it will solve all your problems here.

like image 23
Andrew Janke Avatar answered Oct 04 '22 14:10

Andrew Janke


or you can say:

str = sprintf('%s%s%s', 'red ', 'yellow ', 'white ')
like image 39
Amro Avatar answered Oct 04 '22 16:10

Amro