Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: convert cell of char to cell of vector of doubles

Tags:

matlab

I would like to convert a <1 x 8 cell> of chars

'111001'    '00'    '111000'    '01'    '1111'  '10'    '11101' '110'

to a <1 x 8 cell> of <1 x (length bitcode)> doubles

[111001]    [00]    [111000]    [01]    [1111]  [10]    [11101] [110]

How can I do this?

like image 349
user720491 Avatar asked Dec 18 '12 00:12

user720491


People also ask

How do I convert a cell to a vector 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" .

How do you make a cell array of character vectors in MATLAB?

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.

How do you convert cells in MATLAB?

C = num2cell( A ) converts array A into cell array C by placing each element of A into a separate cell in C . The num2cell function converts an array that has any data type—even a nonnumeric type.


4 Answers

here's a one liner solution:

 a=num2cell(str2double(s))
like image 136
bla Avatar answered Oct 04 '22 21:10

bla


s = {'111001', '00', '111000', '01', '1111', '10', '11101', '110'};
d = cellfun(@(c_) c_ - '0', s, 'UniformOutput', false);

'01234' - '0' will give 1 by 5 double matrix [0, 1, 2, 3, 4] because '01234' is actually char(['0', '1', '2', '3', '4']), and minus operation between characters will give the operation between their ASCII codes.

like image 33
dlimpid Avatar answered Oct 04 '22 20:10

dlimpid


Try this:

    s = {'111001','00','111000','01','1111','10','11101','110'}
    num = str2num(str2mat(s));
like image 34
Spectre Avatar answered Oct 04 '22 20:10

Spectre


Try using str2num to convert char arrays (strings) to numbers.

If you want to interpret the numbers as binary (base 2) numbers, try using bin2dec.

like image 40
Brian L Avatar answered Oct 04 '22 21:10

Brian L