lola
lola

Reputation: 5789

convert cell to a list of element separated by comma MATLAB

var is 1x6 cell

var = 

'a'  'b'  'v'  'g'  'd'  'r'

I would like to convert it to obtain a list with sparated comma 'a','b','v','g','d','r'

any idea ?

Thanks

Upvotes: 1

Views: 4001

Answers (4)

Flyto
Flyto

Reputation: 686

Much nicer, from Matlab 2013a onwards:

>> c = {'a', 'b', 'v', 'g', 'd', 'r'};
>> strjoin( c, ', ')

ans =

'a, b, v, g, d, r'

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112679

Clumsy one-liner:

cell2mat(strcat(var, [mat2cell(repmat(',',1,length(var)-1), 1, ones(1,length(var)-1)),{''}]))

Upvotes: 0

Robert Seifert
Robert Seifert

Reputation: 25232

These two lines will do it:

c = {'a', 'b', 'v', 'g', 'd', 'r'};
d = [c',[repmat({','},numel(c)-1,1);{[]}]]';
e = [d{:}]

returns:

e = a,b,v,g,d,r

Upvotes: 1

P0W
P0W

Reputation: 47794

Using cell2mat

>> c={'a', 'b', 'v', 'g', 'd', 'r'}

c = 

    'a'    'b'    'v'    'g'    'd'    'r'

>> s=cell2mat(c)

s =

abvgdr

Upvotes: 1

Related Questions