Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 628
Martin08 Avatar asked Mar 13 '11 21:03

Martin08


1 Answers

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

like image 117
Alex Avatar answered Sep 19 '22 14:09

Alex