Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

putting a space in string created with strcat

596 views
Skip to first unread message

Silvia Appendino

unread,
Jan 26, 2010, 10:01:20 AM1/26/10
to
Hello,
I'm trying to create a string with strcat but inserting a space in the middle.. does anyone know how to get this done? the space always dissapears!!

str1='name'
str2='sirname'

what I want is to get
str3='name sirname'

I tried with:
str3=strcat(str1,str2)
str3=strcat(str1,' ',str2)
str3=strcat(str1,'\t',str2)
but with all these it doesn't work!

any hints?
thanks for help!!
silvia

Steven Lord

unread,
Jan 26, 2010, 10:06:04 AM1/26/10
to

"Silvia Appendino" <silvia.a...@polito.it> wrote in message
news:hjn040$j2p$1...@fred.mathworks.com...

> Hello,
> I'm trying to create a string with strcat but inserting a space in the
> middle.. does anyone know how to get this done? the space always
> dissapears!!

This is expected behavior. Read the third paragraph in the Description
section of the reference page for STRCAT:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html

a = 'hello ';
b = 'world';
[a b]
strcat(a, b)
strcat({a}, {b})

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Titus Edelhofer

unread,
Jan 26, 2010, 10:07:02 AM1/26/10
to
Hi Silvia,
use [] instead:
str3 = [str1 ' ' str2];

Titus

"Silvia Appendino" <silvia.a...@polito.it> schrieb im Newsbeitrag
news:hjn040$j2p$1...@fred.mathworks.com...

dpb

unread,
Jan 26, 2010, 10:13:04 AM1/26/10
to
Silvia Appendino wrote:
> Hello,
> I'm trying to create a string with strcat but inserting a space in the
> middle.. does anyone know how to get this done? the space always
> dissapears!!
>
> str1='name'
> str2='sirname'
>
> what I want is to get str3='name sirname'
>
...
Per documentation...

STRCAT Concatenate strings.
T = STRCAT(S1,S2,S3,...) horizontally concatenates ...
... The trailing padding is ignored.

cat() will honor either embedded or trailing spaces or a blank string
placeholder

Probably simpler at least one way would be sprintf() letting the format
string handle it if the variables don't have trailing blanks. If they
do, those will be included as well.

>> sprintf('%s %s', 'name', 'surname')
ans =
name surname

--

0 new messages