9
>> 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
Praetorian
  • 106,671
  • 19
  • 240
  • 328
user1532230
  • 157
  • 1
  • 2
  • 7

3 Answers3

12
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
tmpearce
  • 12,523
  • 4
  • 42
  • 60
6

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}];
Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
1

Well, you have mixed data types here so there isn't a very straight forward way to do it.

The easiest way I can think of, if you know where the data is you want, is to simply use cell2mat

IE: cell2mat(C(1,1)) would return 1 as a double.

Ben A.
  • 1,039
  • 6
  • 13