Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cell2mat with different data types and empty cells

1,246 views
Skip to first unread message

Jerry Martinez

unread,
Jan 17, 2013, 4:12:07 PM1/17/13
to
I was reading an old post and replied to it to see if someone could help, because I have a similar problem, but now I decided to post question anew.

In response to a question, an author (Andrei Bobrov?) wrote:
> "that sounds strange as ML usually (intrinsically) takes care of conversions for differing data types..."

I don't believe what Matlab author wrote (re-typed above) is correct because I get the following message:
"Error using cell2mat (line 46)
All contents of the input cell array must be of the same data type."

I had to write a function that converted each element to a string, whether it was numeric, or a NaN.

Then, when I pass the cell array through my "string converter", cell2mat complains about the cells not being consistent. I presume this is because I have empty fields (in the last rows of the arrays) that make the column appear shorter.

"CAT arguments dimensions are not consistent.

Error in cell2mat (line 84)
m{n} = cat(1,c{:,n});

Error in MergeTables_26 (line 820)
mat_Prior_Week = cell2mat(strcell_Prior_Week);"

Bruno Luong

unread,
Jan 17, 2013, 5:36:08 PM1/17/13
to
Few tips:

> c={uint8(8) pi []}

c =

[8] [3.1416] []

>> c(cellfun('isempty',c)) = {NaN} % Remove empty cells

c =

[8] [3.1416] [NaN]

>> [c{:}] % convert to lower class

ans =

8 3 0

>> cellfun(@double, c) % convert to specific class

ans =

8.0000 3.1416 NaN

% Bruno

Jerry Martinez

unread,
Jan 17, 2013, 6:27:08 PM1/17/13
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <kd9uco$ejl$1...@newscl01ah.mathworks.com>...
Thank you, Bruno. I'm past having heartburn with NaN's. It occurred to me that I should post an example. Here's the sample cell (4 rows x 5 col ) giving me trouble right now (note that I've inserted two single quotes where a cell is empty) :


C={['Office'] ['Job'] ['Job Description'] ['Task Code']['Task Description'];
['5AUS'] ['0052'] ['TEST FOR KRONOS UPLOAD '] [ '' ] [ '' ];
['5AUS'] ['0052'] ['TEST FOR KRONOS UPLOAD '] [1000] ['GENERAL'];
['5AUS'] ['0390'] ['Different data for TEST '] [ '' ] [ '' ] }

I saw you posted an example removing the empty cells, but I need my cell to keep the same dimensions. I tried inserting the actual words "blank" in the cell before calling "cell2mat" function and it still failed with the same error "Error using cat
CAT arguments dimensions are not consistent.".

Thanks

Bruno Luong

unread,
Jan 17, 2013, 6:42:09 PM1/17/13
to
>
> C={['Office'] ['Job'] ['Job Description'] ['Task Code']['Task Description'];
> ['5AUS'] ['0052'] ['TEST FOR KRONOS UPLOAD '] [ '' ] [ '' ];
> ['5AUS'] ['0052'] ['TEST FOR KRONOS UPLOAD '] [1000] ['GENERAL'];
> ['5AUS'] ['0390'] ['Different data for TEST '] [ '' ] [ '' ] }
>

A matrix contains exclusively numeric data. So you probably need first to explain how you even see such cell can miraculously metamorphose into matrix.

Bruno

Jerry Martinez

unread,
Jan 18, 2013, 8:40:08 AM1/18/13
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <kda28h$qrl$1...@newscl01ah.mathworks.com>...
Ahh, I see. That may explain it. I was looking for a pink elephant.
0 new messages