From: james bejon on
Dear All, I'm sure this is really easy, but it's driving me nuts. I can't find a way of replacing the NaNs in a cell array like

c = {'geasgeas', NaN, 1, 'feafea'};

with blanks, i.e. with {''}. My first attempt was to use

cellfun(@isnan, c, 'Uni', 0)

as an index, but unfortunately this expression interrogates every element of every cell in the array. And

cellfun(@isnumeric, c, 'Uni', 0)

returns 1 for NaNs, so doesn't help much either.
From: Steven Lord on

"james bejon" <jamesbejon(a)yahoo.co.uk> wrote in message
news:htj6p1$1i$1(a)fred.mathworks.com...
> Dear All, I'm sure this is really easy, but it's driving me nuts. I can't
> find a way of replacing the NaNs in a cell array like
>
> c = {'geasgeas', NaN, 1, 'feafea'};
>
> with blanks, i.e. with {''}. My first attempt was to use
>
> cellfun(@isnan, c, 'Uni', 0)
>
> as an index, but unfortunately this expression interrogates every element
> of every cell in the array. And
>
> cellfun(@isnumeric, c, 'Uni', 0)
>
> returns 1 for NaNs, so doesn't help much either.

Your first attempt was close -- you just need an ALL in there.

c = {'geasgeas', NaN, 1, 'feafea'};
r = cellfun(@(x) all(isnan(x)), c);
c(r) = {''}

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: james bejon on
Very nice. Thanks.
From: us on
"james bejon" <jamesbejon(a)yahoo.co.uk> wrote in message <htj6p1$1i$1(a)fred.mathworks.com>...
> Dear All, I'm sure this is really easy, but it's driving me nuts. I can't find a way of replacing the NaNs in a cell array like
>
> c = {'geasgeas', NaN, 1, 'feafea'};
>
> with blanks, i.e. with {''}. My first attempt was to use
>
> cellfun(@isnan, c, 'Uni', 0)
>
> as an index, but unfortunately this expression interrogates every element of every cell in the array. And
>
> cellfun(@isnumeric, c, 'Uni', 0)
>
> returns 1 for NaNs, so doesn't help much either.

one of the many solutions

c = {'a',nan,1,'bb',[1,nan,2]}; % <- note: added one more CELL...
r1=c(~cellfun(@(x) all(isnan(x)),c)) % <- ALL()
% r1 = 'a' [1] 'bb' [1x3 double]
% -and-
r2=c(~cellfun(@(x) any(isnan(x)),c)) % <- ANY()
% r2 = 'a' [1] 'bb'

us