Replacing NaN from doubles in a cell array with blank

6 views (last 30 days)
"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
Image Analyst
Image Analyst on 31 Oct 2014
Edited: Image Analyst on 31 Oct 2014
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
Adrian
Adrian on 1 Nov 2014
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).
Ced
Ced on 2 Nov 2014
Edited: Ced on 2 Nov 2014
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

Community Treasure Hunt

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

Start Hunting!