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

后端 未结 5 745
青春惊慌失措
青春惊慌失措 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:12

    Both join and strjoin are introduced in R2013a. However, the mathworks site about strjoin reads:

    Starting in R2016b, the join function is recommended to join elements of a string array.

    >> C = {'one','two','three'};
    >> join(C) %same result as: >> join(C, ' ')
    
    ans = 
    
      string
    
        "one two three"
    
    >> join(C, ', and-ah ')
    
    ans = 
    
      string
    
        "one, and-ah two, and-ah three"
    

    Personally I like Alex' solution as well, as older versions of Matlab are abundant in research groups around the world.

提交回复
热议问题