Replacing NaN from doubles in a cell array with blank

"Result" consists of a cell array (19x2) with doubles (75x10). I'd like to replace all the NaN in the the columns (:,2) of all cells (Result{1:25,1:2}) with blanks using cellfun.
Result(cellfun(@(x) any(isnan(x(:,2))),Result)) = {''}
is what I've tried but it blanks me the whole cell and not the double.
Thank you in advance for any help!

2 Comments

Can you make it easy for us to help you ? Can you attach a mat file with your Result cell array inside it? It would make it easier for people to try things.

Sign in to comment.

Answers (1)

Ced
Ced on 31 Oct 2014
Edited: Ced on 31 Oct 2014
What exactly do you mean by "replacing with blanks"? Do you want to delete the whole row? or replace by 0? Your matrix consists of doubles, so you can't simply delete certain elements, otherwise the different columns of your matrix would have different lengths.
Do you have to use cellfun? Otherwise, simply do
[ nrows, ncols ] = size(Result);
for j = 1:ncols
for i = 1:nrows
Result{i,j}(isnan(Result{i,j}(:,2)),2) = 0;
end
end
I don't know how you can directly perform assignments in cellfun, but if you absolutely want to use it, you could do:
function x = set_zero_if_nan(x,col)
x(isnan(x(:,col)),col) = 0;
end
and then
Result = cellfun( @(x) set_zero_if_nan(x,2), Result, 'UniformOutput',0 );

4 Comments

Thank you, Ced. No, I don't have to use cellfun, it just seemed a promising way to solve my NaN-problem. The for-loop works fine but I don't want to replace the NaNs with 0 since I later will use the cell array for polyfitting and don't want MATLAB to calculate and include the "0" for/in the plot. I neither want MATLAB to plot the NaNs that's why I just want to replace the NaNs with a blank double.
There was a question asked before which is similar to my problem now but only dealt with doubles instead of cell arrays: http://www.mathworks.ch/matlabcentral/answers/90172-replace-nan-with-blanks
Is there any solutions/workarounds to my problem?
There's no such thing as a blank double. You have to use NaN to indicate missing data. You may need to unpack individual rows or columns from the numeric matrices, then remove the NaNs, then call polyfit.
That's a pity... So, I'm going to rephrase my question: Is there a way for "polyfit" which will ignore the NaNs (something like "nanpolyfit")?
I've eliminated all the NaNs as you've suggested, David. The problem here is that I'm ending up with doubles of different lengths which results in an error message when plotting the polyfitted data ("Vectors must be the same lengths"). Therefore, I have to preserve the length of the doubles (=75).
What input are you giving to polyfit? x(:,2) of all cells?
Polyfit doesn't really fit a matrix, it simply uses all points to fit. Meaning, instead of trying to have blanks in your matrix X = [ x1 x2 x3 ... ] which is impossible, simply pass
X = X(:); % save all as one long vector
X = X(~isnan(X)); % eliminate all nan
to polyfit, where X was the matrix you were trying to pass earlier.

Sign in to comment.

Products

Asked:

on 31 Oct 2014

Edited:

Ced
on 2 Nov 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!