How to join a string cellarray row by row with a specified delimiter
11 views (last 30 days)
Show older comments
MathWorks Support Team
on 12 Jun 2018
Answered: MathWorks Support Team
on 13 Jun 2018
If i have a cellarray "text",
text = {
'a' 'b' 'c'
'X' 'Y' 'z'
'1' '2' '3'
}; % a 3x3 cell
the desired output should be in a row by row concatenation with a comma:
'a,b,c'
'X,Y,z'
'1,2,3'
however,
result = strjoin(text,',')
will only give a result like this:
'a,X,1,b,Y,2,c,z,3'
Accepted Answer
MathWorks Support Team
on 12 Jun 2018
Starting from R2016b this could be solved rather easily: join(text,','). However, "join" was implemented differently before 2016b and it will give a result like this: 'a,X,1,b,Y,2,c,z,3' (not a row by row concatenation as the customer desires)
The simplest solution before 2016b will just be using a for loop to concatenate the cell array row by row as vectorization is not possible.
result = cell(size(text,1), 1);
for i = 1: size(text, 1)
result(i, :) = {strjoin(text(i, :), ',')};
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!