tags:

views:

264

answers:

3

I have files which have many empty cells which appear as NaNs when I use cell2mat but the problem is when I need to get the average values I cannot work with this as it shows error with NaN.

In excel it overlooks, how do i do the same in MATLAB?

#

Addition to above question:

It seems simple. I am writing a file using xlswrite xlswrite('test.xls',M);

I have data in all rows except 1 how do i write M(1,:)= ( 'time', 'count', length','width') i want M(1,1)=time, M(1,2)= count and so on i have data from M(2,1) to M(10,20)

Any help?

+2  A: 

Use ' isfinite ' function to get rid of all NaN and infinities

A=A(isfinite(A))

%create the cell array containing the column headers columnHeader = {'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5',' '};

%write the column headers first xlswrite('myFile1.xls', columnHeader );

% write the data directly underneath the column headers xlswrite('newFile.xls',M,'Sheet1','A2');

AP
Thanks it worked!!
Paul
>check out:>http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlswrite.html
AP
+1  A: 

Statistics Toolbox has several statistical functions to deal with NaN values. See nanmean, nanmedian, nanstd, nanmin, nanmax, etc.

yuk
By the way, min/max work the same as nanmin/nanmax. It is for mean and median where you have to use the nanX functions.
Jonas
+6  A: 

As AP correctly points out, you can use the function ISFINITE to find and keep only finite values in your matrix. You can also use the function ISNAN. However, removing values from your matrix can have the unintended consequence of reshaping your matrix into a row or column vector:

>> mat = [1 2 3; 4 NaN 6; 7 8 9]  %# A sample 3-by-3 matrix

mat =

     1     2     3
     4   NaN     6
     7     8     9

>> mat = mat(~isnan(mat))  %# Removing the NaN gives you an 8-by-1 vector

mat =

     1
     4
     7
     2
     8
     3
     6
     9

Another alternative is to use some functions from the Statistics Toolbox (if you have access to it) that are designed to deal with matrices containing NaN values. Since you mention taking averages, you may want to check out NANMEAN:

>> mat = [1 2 3; 4 NaN 6; 7 8 9];
>> nanmean(mat)

ans =

     4     5     6     %# The column means computed by ignoring NaN values



EDIT: To answer your additional question on the use of XLSWRITE, this sample code should illustrate one way you can write your data:

C = {'time','count','length','width'};  %# A cell array of strings
M = rand(10,20);                        %# A 10-by-20 array of random values
xlswrite('test.xls',C);           %# Writes C to cells A1 through D1
xlswrite('test.xls',M,'A2:T11');  %# Writes M to cells A2 through T11
gnovice
+1 for pointing out the reshape problem
Jonas
Thanks , however i did not try this as the isfinite worked for me ,as all i wanted was to get averages.
Paul
@Paul: If the data you're averaging is just a 1-D vector, then you wouldn't have to worry about the reshaping issue I address above. The simple solution from AP is all you'd really need. ;)
gnovice
@Paul: I added an example to my answer addressing your additional question.
gnovice
@gnoice : looking at your solution. It still changes the size from 3X3 to 8x1 matrix..Is there a way to get rid of rows which has NaN?i mean if there is a NaN , like in row two , it deletes it and gives a 2X3 matrix
AP
@AP: If you want to delete an *entire* row if there is even 1 NaN value there, the following should work: `mat = mat(~any(isnan(mat),2),:);`
gnovice
@gnoice : Ialways have problem with cell and num. this command isnan does not work with cells. ' num2cell' converts to cell , how do you get back? ' cell2mat' ??
AP
@AP: CELL2MAT or CAT can be used to convert a cell array back to a numeric matrix. If you wanted to remove the rows of a cell array that contain at least 1 cell with a NaN value, you could instead do the following: `mat = mat(~any(cellfun(@isnan,mat),2),:);`
gnovice
@gnoice: Thanks
AP

related questions