Thread Subject: cellfun and cell2mat

Subject: cellfun and cell2mat

From: bruna mari

Date: 22 May, 2008 09:29:02

Message: 1 of 5

I have a code that is supposed to apply the function
cell2mat to a matrix called funds (monthly return of funds)
from excel.

when I do cell2mat (funds) I receive:
??? Error using ==> cell2mat at 47
All contents of the input cell array must be of the same
data type.

and at 47 it is:
Error out if cell array contains mixed data types

cellclass = class(c{1});
ciscellclass = cellfun('isclass',c,cellclass);
~all(ciscellclass(:)) and I got ans=1 for this.

if I do class (funds)the answer is = cell. And if I do
class (funds (1,1)), or any other position the ans is cell.

the matrix is a 107x2, with the 1,1 and 1,2 positions being
the names of the 2 funds, and the rest being the returns.

How can I get the error for cell2mat, and how can the
answer for cellfun be subclass, and not class if class
(funds) for all the positions is cell??

How can I fix it???

Subject: cellfun and cell2mat

From: John D'Errico

Date: 22 May, 2008 11:19:02

Message: 2 of 5

"bruna mari" <bruna.mari@forsythpartners.com> wrote in message
<g13ecu$d43$1@fred.mathworks.com>...
> I have a code that is supposed to apply the function
> cell2mat to a matrix called funds (monthly return of funds)
> from excel.
>
> when I do cell2mat (funds) I receive:
> ??? Error using ==> cell2mat at 47
> All contents of the input cell array must be of the same
> data type.
>
> and at 47 it is:
> Error out if cell array contains mixed data types
>
> cellclass = class(c{1});
> ciscellclass = cellfun('isclass',c,cellclass);
> ~all(ciscellclass(:)) and I got ans=1 for this.
>
> if I do class (funds)the answer is = cell. And if I do
> class (funds (1,1)), or any other position the ans is cell.
>
> the matrix is a 107x2, with the 1,1 and 1,2 positions being
> the names of the 2 funds, and the rest being the returns.
>
> How can I get the error for cell2mat, and how can the
> answer for cellfun be subclass, and not class if class
> (funds) for all the positions is cell??
>
> How can I fix it???

But if there are two cells that are strings,
and the rest of the cells are numeric data,
then you CANNOT form this cell array
into a numeric array. The error message
clearly states this fact.

Double arrays CANNOT hold mixed types
of data. This is why we have cell arrays.

If you strip off the string elements from
your cell array and only apply cell2mat
to the purely numeric part which remains,
then it will work.

HTH,
John

Subject: cellfun and cell2mat

From: Ken Fleisher

Date: 22 May, 2008 11:55:04

Message: 3 of 5

> if I do class (funds)the answer is = cell. And if I do
> class (funds (1,1)), or any other position the ans is cell.

class(funds{i,j})

Try this for all of your cells and I think you'll find your
answer. You found that the class of each element is a cell,
but you did not test for what class the contents of each
cell is, and that's where you are getting the error.

Ken

Subject: cellfun and cell2mat

From: bruna mari

Date: 22 May, 2008 12:41:02

Message: 4 of 5

"Ken Fleisher" <k-fleisher.donotspamme@nga.gov> wrote in
message <g13muo$4ml$1@fred.mathworks.com>...
> > if I do class (funds)the answer is = cell. And if I do
> > class (funds (1,1)), or any other position the ans is
cell.
>
> class(funds{i,j})
>
> Try this for all of your cells and I think you'll find
your
> answer. You found that the class of each element is a
cell,
> but you did not test for what class the contents of each
> cell is, and that's where you are getting the error.
>
> Ken

Hi,

Thanks!

So I can' t use cell2mat in a matrix with headers
containing chars...

But the code was working before the system changed from XP
to Vista, once I changed the system, even maintaining the
excel 2003 and the Matlab R2007a its not working anymore....

Any ideas of how to transform the matrix in only one type
of data???

Subject: cellfun and cell2mat

From: John D'Errico

Date: 22 May, 2008 14:40:19

Message: 5 of 5

"bruna mari" <bruna.mari@forsythpartners.com> wrote in message
<g13pku$774$1@fred.mathworks.com>...
> "Ken Fleisher" <k-fleisher.donotspamme@nga.gov> wrote in
> message <g13muo$4ml$1@fred.mathworks.com>...
> > > if I do class (funds)the answer is = cell. And if I do
> > > class (funds (1,1)), or any other position the ans is
> cell.
> >
> > class(funds{i,j})
> >
> > Try this for all of your cells and I think you'll find
> your
> > answer. You found that the class of each element is a
> cell,
> > but you did not test for what class the contents of each
> > cell is, and that's where you are getting the error.
> >
> > Ken
>
> Hi,
>
> Thanks!
>
> So I can' t use cell2mat in a matrix with headers
> containing chars...
>
> But the code was working before the system changed from XP
> to Vista, once I changed the system, even maintaining the
> excel 2003 and the Matlab R2007a its not working anymore....
>
> Any ideas of how to transform the matrix in only one type
> of data???

No. I can absolutely assure you that cell2mat
did NOT work previously on a cell array of
this form. Vista versus XP is not relevant.

As I said, drop off the elements that are not
numeric, then apply cell2mat to the remainder.

John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
class bruna mari 22 May, 2008 05:30:11
cellfun bruna mari 22 May, 2008 05:30:11
cell2mat bruna mari 22 May, 2008 05:30:10
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com