Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Remove Nan from cell array?

Subject: Remove Nan from cell array?

From: Alaa Elwany

Date: 30 Nov, 2011 20:52:09

Message: 1 of 7

Hi,

I have a cell array that has either strings or NaN in it.

e.g.

A =
'X'
'Y'
'Z'
[NaN]
[NaN]


How can I remove the NaN elements of the cell array?

I tried:
TT(isnan([TT{:}]))=[]

But it doesn't work. Help is appreciated. Thanks.

Subject: Remove Nan from cell array?

From: ade77

Date: 30 Nov, 2011 21:12:08

Message: 2 of 7

try this:
 A(cellfun(@isnan, A)) = []

other options exist.

Subject: Remove Nan from cell array?

From: Alaa Elwany

Date: 1 Dec, 2011 07:11:08

Message: 3 of 7

"ade77 " <ade100a@gmail.com> wrote in message <jb6678$fo8$1@newscl01ah.mathworks.com>...
> try this:
> A(cellfun(@isnan, A)) = []
>
> other options exist.

Thanks a lot ade77. Your method, indeed, works for the example A cell array I listed as a demonstration. I am really troubled though, because when I tried it for the cell array that I have in my work space, it gave the following error message:

??Error Using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false

The cell array in my work space actually has the form:

A =
'F1'
'F1'
'F2'
'F2'
'F2'
'F3'
'F3'
'F3'
[NaN]
[NaN]
[NaN]

and I need to be able to remove NaN's from the array. Do you see this error message related to the fact that the strings are not recognized as strings due to the numbers in them?

It is driving me crazy!

Thanks a lot for your concern.

Subject: Remove Nan from cell array?

From: Alaa Elwany

Date: 1 Dec, 2011 07:19:08

Message: 4 of 7

"ade77 " <ade100a@gmail.com> wrote in message <jb6678$fo8$1@newscl01ah.mathworks.com>...
> try this:
> A(cellfun(@isnan, A)) = []
>
> other options exist.

Oh, and when I tried to set UniformOutput to False, it gave another error message:

Error using ==> Subindex
Function 'Subindex' is not defined for values of class 'cell'

Subject: Remove Nan from cell array?

From: Steven_Lord

Date: 1 Dec, 2011 14:36:42

Message: 5 of 7



"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message
news:jb79ac$j9$1@newscl01ah.mathworks.com...
> "ade77 " <ade100a@gmail.com> wrote in message
> <jb6678$fo8$1@newscl01ah.mathworks.com>...
>> try this:
>> A(cellfun(@isnan, A)) = []
>>
>> other options exist.
>
> Thanks a lot ade77. Your method, indeed, works for the example A cell
> array I listed as a demonstration. I am really troubled though, because
> when I tried it for the cell array that I have in my work space, it gave
> the following error message:
>
> ??Error Using ==> cellfun
> Non-scalar in Uniform output, at index 1, output 1.
> Set 'UniformOutput' to false
>
> The cell array in my work space actually has the form:
>
> A =
> 'F1'
> 'F1'
> 'F2'
> 'F2'
> 'F2'
> 'F3'
> 'F3'
> 'F3'
> [NaN]
> [NaN]
> [NaN]
>
> and I need to be able to remove NaN's from the array. Do you see this
> error message related to the fact that the strings are not recognized as
> strings due to the numbers in them?

No, the problem is that 'F1' is not scalar so isnan('F1') returns [false
false] and CELLFUN, when called the way you did, expects the output of the
function it invokes on the cell array to be scalar [or a cell.] See the
description of the UniformOutput parameter in the documentation for CELLFUN
and note its default value:

http://www.mathworks.com/help/techdoc/ref/cellfun.html

The third example on that page shows how to use UniformOutput to solve this
in the general case, but since you do actually WANT the output of CELLFUN to
be a numel(A)-by-1 logical vector [so that you can use it for logical
indexing] I would instead use ISNAN in conjunction with ALL:

A = {'F1'; NaN; NaN; 'F2'; 'CSSM'};
fh = @(x) all(isnan(x(:)));
A(cellfun(fh, A)) = [];
A

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Remove Nan from cell array?

From: Alaa Elwany

Date: 1 Dec, 2011 17:53:08

Message: 6 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <jb83dq$i7e$1@newscl01ah.mathworks.com>...
>
>
> "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message
> news:jb79ac$j9$1@newscl01ah.mathworks.com...
> > "ade77 " <ade100a@gmail.com> wrote in message
> > <jb6678$fo8$1@newscl01ah.mathworks.com>...
> >> try this:
> >> A(cellfun(@isnan, A)) = []
> >>
> >> other options exist.
> >
> > Thanks a lot ade77. Your method, indeed, works for the example A cell
> > array I listed as a demonstration. I am really troubled though, because
> > when I tried it for the cell array that I have in my work space, it gave
> > the following error message:
> >
> > ??Error Using ==> cellfun
> > Non-scalar in Uniform output, at index 1, output 1.
> > Set 'UniformOutput' to false
> >
> > The cell array in my work space actually has the form:
> >
> > A =
> > 'F1'
> > 'F1'
> > 'F2'
> > 'F2'
> > 'F2'
> > 'F3'
> > 'F3'
> > 'F3'
> > [NaN]
> > [NaN]
> > [NaN]
> >
> > and I need to be able to remove NaN's from the array. Do you see this
> > error message related to the fact that the strings are not recognized as
> > strings due to the numbers in them?
>
> No, the problem is that 'F1' is not scalar so isnan('F1') returns [false
> false] and CELLFUN, when called the way you did, expects the output of the
> function it invokes on the cell array to be scalar [or a cell.] See the
> description of the UniformOutput parameter in the documentation for CELLFUN
> and note its default value:
>
> http://www.mathworks.com/help/techdoc/ref/cellfun.html
>
> The third example on that page shows how to use UniformOutput to solve this
> in the general case, but since you do actually WANT the output of CELLFUN to
> be a numel(A)-by-1 logical vector [so that you can use it for logical
> indexing] I would instead use ISNAN in conjunction with ALL:
>
> A = {'F1'; NaN; NaN; 'F2'; 'CSSM'};
> fh = @(x) all(isnan(x(:)));
> A(cellfun(fh, A)) = [];
> A
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Steve,

This is great explanation, and this method actually worked. I thank you VERY much for your help and concern!

Alaa

Subject: Remove Nan from cell array?

From: Jeremy

Date: 6 Jul, 2014 19:05:12

Message: 7 of 7

I know this is an old post, but a simpler method to use if the data to be retained are strings, would be:

A=A(cell2mat(cellfun(@ischar,A,'UniformOutput',0)));

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us