Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert cell to double

>> C = [{1} {2} ; {'@CF'} {2}] 
C = 

[  1]    [2]
'@CF'    [2]


>> whos C
  Name      Size            Bytes  Class    Attributes

  C         2x2               478  cell  

How can I convert C into double so that:

>> C
C = 
1    2
NaN  2

I've tried str2double(C). It returns:

   NaN   NaN
   NaN   NaN
like image 539
user1532230 Avatar asked Jul 17 '12 15:07

user1532230


2 Answers

C = [{1} {2} ; {'@CF'} {2}]

C = 

    [  1]    [2]
    '@CF'    [2]

D = cellfun(@isnumeric,C);
C(~D)={nan}

C = 

    [  1]    [2]
    [NaN]    [2]

cell2mat(C)

ans =

     1     2
   NaN     2
like image 181
tmpearce Avatar answered Oct 09 '22 09:10

tmpearce


Find the non numeric values with isnumeric, queried by cellfun. Use that with logical indexing to extract the numeric values:

C = [{1} {2} ; {'@CF'} {2}];
isnum = cellfun(@isnumeric,C);
result = NaN(size(C));
result(isnum) = [C{isnum}];
like image 31
Gunther Struyf Avatar answered Oct 09 '22 09:10

Gunther Struyf