如何在MATLAB中将单元格数组中的字符串与它们之间的空格连接起来?

Mar*_*n08 13 string matlab whitespace concatenation cell

我想连接(用空格填充)单元格数组{'a', 'b'}中的字符串以提供单个字符串'a b'.我怎样才能在MATLAB中做到这一点?

Ale*_*lex 17

您可以通过将单元格数组用作sprintf函数的一组参数,然后使用strtrim清理多余的空格来作弊:

 strs = {'a', 'b', 'c'};
 strs_spaces = sprintf('%s ' ,strs{:});
 trimmed = strtrim(strs_spaces);
Run Code Online (Sandbox Code Playgroud)

很脏,但我喜欢它......


小智 10

matlab有这个功能,

参考:

strjoin

http://www.mathworks.com/help/matlab/ref/strjoin.html

strjoin

将单元格数组中的字符串加入单个字符串

句法

str = strjoin(C) example

str = strjoin(C,delimiter)
Run Code Online (Sandbox Code Playgroud)

例如:

用空白加入单词列表

使用单个空格将字符串C的单元格数组中的单个字符串连接起来.

C = {'one','two','three'};

str = strjoin(C)

str =

one two three
Run Code Online (Sandbox Code Playgroud)

  • "strjoin"最初是在R2013a版本中引入的 (4认同)

gra*_*tnz 7

Alex对答案的小改进(?)

strs = {'a','b','c'};  
strs_spaces = [strs{1} sprintf(' %s', strs{2:end})];
Run Code Online (Sandbox Code Playgroud)