I have a cell array a = {'a' , 'b' , 'c' ,'d' , 'e'}
I need to convert a cell array to matix with ','(comma in between)
my answer should be a = a,b,c,d,e
how can i do it?
thanks a lot

1 Kommentar

While technically
a = 'a,b,c,d,e';
is a matrix. Calling it a char array or a string would make more sense.
It certainly begs the question: do you understand the differences (or lack of) between a matrix of numbers and a matrix of characters?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 16 Jun. 2015
Bearbeitet: Andrei Bobrov am 16 Jun. 2015

1 Stimme

a = {'a' , 'b' , 'c' ,'d' , 'e'};
a = sprintf('%c,',[a{:}]);
a = a(1:end-1);

5 Kommentare

Andrei, generally correct, but the OP wanted to have a "," in between, so:
a = {'a' , 'b' , 'c' ,'d' , 'e'};
% add a "," after each string:
aCell = strcat(a, ',');
% combine (see above)
aString = [aCell{:}];
% remove the last ",":
aString(end)=[];
Titus
Guillaume
Guillaume am 16 Jun. 2015
Bearbeitet: Guillaume am 16 Jun. 2015
This all seems extremely convoluted. What is wrong with ?
a = {'a', 'b', 'c', 'd', 'e'};
a = strjoin(a, ',') %available since 2013 (a or b?)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan am 16 Jun. 2015
how to perform the same operation if given cell array a = {'abc_def' , 'sdfds_sdaf' , 'ddfs_sdfsd'} answer should be
a = abc_def , sdfds_sdaf , ddfs_sdfsd
thanks a lot
Guillaume
Guillaume am 16 Jun. 2015
See my comment and look at the documentation of strjoin.
Titus Edelhofer
Titus Edelhofer am 17 Jun. 2015
@Guillaume: nothing wrong with strjoin except that I did not know it ;-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by