Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab char array to cell array [duplicate]

say I have an array of chars which looks like....

   hello
   hillo
   hello

and I would like to convert them to a cell array which would be the same as...

     A = {'hello';'hillo';'hello'}

how would I go about doing this, I've tried using mat2cell but it appears to just put everything into one large cell and doesn't really split them up.. So say for example with the original array of chars it would output like this if i put

     A = mat2cell(arrayofchars)

     [3x5 char]

    instead of the preferred output of...

     'hello'
     'hillo'
     'hello'

Sorry if I haven't explained my problem very well! Im quite new to matlab!

Cheers!

like image 671
bdavies6086 Avatar asked Sep 01 '14 11:09

bdavies6086


People also ask

How do you make a cell array of character vectors?

To create a cell array of character vectors, use curly braces, {} , just as you would to create any cell array. For example, use a cell array of character vectors to store a list of names. The character vectors in C can have different lengths because a cell array does not require that its contents have the same size.

How do I convert a string to a cell array in Matlab?

C = cellstr( A ) converts A to a cell array of character vectors. For instance, if A is a string, "foo" , C is a cell array containing a character vector, {'foo'} . C = cellstr( A , dateFmt ) , where A is a datetime or duration array, applies the specified format, such as "HH:mm:ss" .

What is cell2mat in Matlab?

A = cell2mat( C ) converts a cell array into an ordinary array. The elements of the cell array must all contain the same data type, and the resulting array is of that data type. The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined.


1 Answers

You can use the function cellstr to do this, as proposed by Divakar.

A = ['hello'
     'hillo'
     'hello']

C = cellstr(A)
C =     
    'hello'
    'hillo'
    'hello'
like image 95
2 revs Avatar answered Sep 21 '22 23:09

2 revs