From: matal on
[Warning - newbie question]

I have a cellarray that mostly contains numbers but has a few cells containing non-numeric values. I want to convert this to a matrix of doubles containing NaN for the non-numeric cells.

I frankly expected cell2mat to do this as default behavior or at least have a flag to specify it - but cell2mat gives an error.

I then tried this:
x = cellfun( @(xx)(if(isnumber(xx),xx,NaN)), CELLARRAY );

Only to find that Matlab probably doesn't have a conditional operation that I can use in an anonymous functin as I try above.

This seems like something that should be very easy to do - what am I missing?

Thanks in advance!
From: Matt Fig on
One approach:

% Data
C = {1 2;'pipes' 'oil';5 'pizzaz'}

% Engine
C(~cellfun('isclass',C,'double')) = {NaN};
M = cell2mat(C)
From: us on
"matal " <tahsin.alam(a)db.com> wrote in message <hrfemr$sbu$1(a)fred.mathworks.com>...
> [Warning - newbie question]
>
> I have a cellarray that mostly contains numbers but has a few cells containing non-numeric values. I want to convert this to a matrix of doubles containing NaN for the non-numeric cells.
>
> I frankly expected cell2mat to do this as default behavior or at least have a flag to specify it - but cell2mat gives an error.
>
> I then tried this:
> x = cellfun( @(xx)(if(isnumber(xx),xx,NaN)), CELLARRAY );
>
> Only to find that Matlab probably doesn't have a conditional operation that I can use in an anonymous functin as I try above.
>
> This seems like something that should be very easy to do - what am I missing?
>
> Thanks in advance!

one of the many solutions

c={
1,2,'a',3
'b','c',4,'d'
};
cn=c; % <- save a copy...
cn(cellfun(@(x) ~isnumeric(x),c))={nan}
%{
% cn =
[ 1] [ 2] [NaN] [ 3]
[NaN] [NaN] [ 4] [NaN]
%}

us
 | 
Pages: 1
Prev: PL 2303 problem
Next: generate matrix