dbl001
dbl001

Reputation: 2459

Matlab: convert NaNs from xlsread to blank

The SOM 2.1 Toolbox is having issues with NaNs. The code below only works for cell arrays and not double precision arrays of numbers.

[D, headers] = xlsread('Workbook2.xlsx', 'Sheet1', 'A2:BE101');
idx    = isnan(D);
D(idx) = {[]}

The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.

Any suggestions?

Upvotes: 0

Views: 258

Answers (1)

NKN
NKN

Reputation: 6424

First of all you cannot do this : D(idx) = {[]} if you have a double array.

Then, isnan does not work with cell arrays. Consider the following cell:

a = cell(2,2);
a{1,1} = NaN;
a{1,2} = 2;
a{2,1} = NaN;
a{2,2} = 'hi';

you cab either use isnan element-wise (on each element of a cell), like:

isnan(a{1,1})
=
    1

or when all the elements of the cell are the same type, you can use cell2mat function first to convert it and then check all the elements with isnan at once, such as:

a = cell(2,2);
a{1,1} = NaN;
a{1,2} = 2;
a{2,1} = NaN;
a{2,2} = 3;

c=cell2mat(a)

c =

   NaN     2
   NaN     3

isnan(c)

ans =

     1     0
     1     0

So the answer depends on the xls sheet that you have, and the type of the data. You may use one of the mentioned options.

Update

An example if you are using double array:

>> D = rand(2,3);
>> D(2,2) = NaN

D =

    0.8147    0.1270    0.6324
    0.9058       NaN    0.0975

>> idx = isnan(D)

idx =

     0     0     0
     0     1     0

>> D(idx) = []

D =

  Columns 1 through 3

    0.8147    0.9058    0.1270

  Columns 4 through 5

    0.6324    0.0975

Of course, it breaks the structure of the matrix into a vector.

Upvotes: 1

Related Questions