user720491
user720491

Reputation: 599

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

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?

Upvotes: 7

Views: 5910

Answers (4)

bla
bla

Reputation: 26069

here's a one liner solution:

 a=num2cell(str2double(s))

Upvotes: 8

Spectre
Spectre

Reputation: 684

Try this:

    s = {'111001','00','111000','01','1111','10','11101','110'}
    num = str2num(str2mat(s));

Upvotes: 4

dlimpid
dlimpid

Reputation: 189

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.

Upvotes: 6

Brian L
Brian L

Reputation: 3251

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.

Upvotes: 3

Related Questions