How to insert space between strings while doing strcat ?

for eg:
a='hello'; b='world'; strcat(a,b);
strcat(a,b) gives 'helloworld' But I need it as 'hello world' How can I do that ?

 採用された回答

Star Strider
Star Strider 2015 年 10 月 31 日

9 投票

Use the square bracket [] concatenation operator, and including a space variable is the easiest way:
a='hello';
b='world';
s = ' ';
Result = [a,s,b]
Result =
hello world

4 件のコメント

Susan G
Susan G 2019 年 7 月 3 日
Thank you Star Strider for giving an explanation that not only can be understood by the beginner user of MATLAB, but also one that is easily implemented across a variety of coding needs. I was having trouble creating a function handle when receiving a user entered equation in GUI (received through the get command) and then using that function handle to find a definite integral and other calculated values (i.e. Riemann sums). I used your answer to create an anonymous function and then used str2func to get a function handle that I could use.
Brilliant answer! Thank you!!!
Star Strider
Star Strider 2019 年 7 月 3 日
As always, my pleasure!
I very much appreciate your compliment!
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 4 月 26 日
Thank you Star, you know, the most simple things are always underestimated, but actually the most difficult to found.
Best!
Star Strider
Star Strider 2021 年 4 月 26 日
@Giuseppe Degan Di Dieco — My pleasure!

サインインしてコメントする。

その他の回答 (4 件)

Hugh
Hugh 2017 年 11 月 21 日

15 投票

I like to use the ASCII space character for this situation, code "32". for the OP: strcat(a,32,b) Extension: I would be inclined to also include a comma, code "44": strcat(a,44,32,b)
I look up the characters at: http://www.asciitable.com/

1 件のコメント

Stephen23
Stephen23 2025 年 1 月 31 日
a='hello';
b='world';
strcat(a,44,32,b)
ans = 'hello, world'

サインインしてコメントする。

Walter Roberson
Walter Roberson 2018 年 5 月 5 日

11 投票

There is a trick to strcat. Notice from the documentation,
"For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell and string array inputs, strcat does not remove trailing white space."
This means that if you have
strcat(a, ' ', b)
then the "trailing" space of the ' ' input will be removed, which will have the effect of running the entries together. The trick is to use
strcat(a, {' '}, b)

4 件のコメント

Renwick Beattie
Renwick Beattie 2019 年 2 月 6 日
A note to say that if you are using strcat on multiple line cell arrays then the {' '} needs replicated to the same size of cell array (not character array) - e.g. cellstr(repmat({' '},size(a))) if a is the cell array.
Walter Roberson
Walter Roberson 2019 年 2 月 6 日
a = {'hello';'bye'};
b = {'dolly';'louis'};
>> strcat(a, {' '}, b)
ans =
2×1 cell array
{'hello dolly'}
{'bye louis' }
No need to replicate the {' '}
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 4 月 26 日
Hello everybody,
all your solutions are brilliant, and lead to the same result.
Thanks for your help and time!
Paul Safier
Paul Safier 2025 年 1 月 30 日
@Walter Roberson this is a nice way to do it.

サインインしてコメントする。

Stephen23
Stephen23 2015 年 11 月 1 日
編集済み: Stephen23 2021 年 4 月 26 日

0 投票

The most efficient approach is to use sprintf:
>> a = 'hello';
>> b = 'world!';
>> sprintf('%s %s',a,b)
hello world!
Larissa Bene
Larissa Bene 2018 年 5 月 5 日

0 投票

strcat(string1, " "); strcat(string1, string2);

2 件のコメント

Renwick Beattie
Renwick Beattie 2019 年 2 月 6 日
I get the following error on the " character
Error: The input character is not valid in MATLAB
statements or expressions.
Walter Roberson
Walter Roberson 2019 年 2 月 6 日
" is only valid in MATLAB from R2017a onwards. string() objects started existing in R2016b, but the input syntax of " was not enabled until R2017a.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2015 年 10 月 31 日

コメント済み:

2025 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by