How can I concatenate strings in a cell array with spaces between them in MATLAB?

后端 未结 5 743
青春惊慌失措
青春惊慌失措 2020-12-15 23:34

I want to concatenate (padding with spaces) the strings in a cell array {\'a\', \'b\'} to give a single string \'a b\'. How can I do this in MATLAB

5条回答
  •  星月不相逢
    2020-12-16 00:08

    You can cheat a bit, by using the cell array as a set of argument to the sprintf function, then cleaning up the extra spaces with strtrim:

     strs = {'a', 'b', 'c'};
     strs_spaces = sprintf('%s ' ,strs{:});
     trimmed = strtrim(strs_spaces);
    

    Dirty, but I like it...

提交回复
热议问题