File Exchange

image thumbnail

cell2num

version 1.0 (756 Bytes) by

Used to convert 2 dimensional cell array of single numbers to a matrix.

1.88889
7 Ratings

14 Downloads

Updated

No License

Function to convert an all numeric cell array to a double precision array
********************************************
Usage:
outputmatrix=cell2num(inputcellarray)
********************************************
Output matrix will have the same dimensions as the input cell array
Non-numeric cell contest will become NaN outputs in outputmat
This function only works for 1-2 dimensional cell arrays of single numbers

Comments and Ratings (10)

Grae Abbott

Function works as specified, most people who give bad reviews I think don't understand how this function is supposed to work maybe, or maybe the updated version is better.

Yuriy Monko

Lube F

Lube F (view profile)

Same here. Function is now pointless.

Kieran DSouza

The function must have been updated after the earlier reviews were written. This function is not working. I'm not sure why it would work. I just get a bunch of NaN's which is what I would expect after reading the code. The code returns NaN if the input cell is non-numeric. Since the input is a cell, it always returns NaN

Phillip Feldman

Very handy. This really should be part of the Matlab base.

Mohammad Reza Jahanbin

slightly more compact version of the same thing which work for n-d arrays

cell2num = @(x) reshape(cat(1,x{:}), size(x));

/m

C Schwalm

I should add that I stumbled upon this looking for something else and that, to fully sastisfy my need to get at cell arrays of cell arrays with numeric values, I added

 elseif isnumeric(str2num(char(inputcell{r,c})));
            outputmat(r,c)=str2num(char(inputcell{r,c}));

to your if-then construct. Again, I'd love for this to be built-in or vectorized and welcome anything that barks up this tree like your submission.

C Schwalm

Yes, this is a wonderful thing. I would prefer to see this functionality be integrated into CELL2MAT. But it is not. I often have to extract numeric contents from a cell array of cell arrays (particularly when using regexpi). This is handy.

Darren Brown

This function, although less powerful, is more tolerant than cell2mat because it can be used to extract numeric values from a cell array of mixed data types. Cell arrays are typically used to store strings along with numeric data. This function can retrieve just the numeric data.

John D'Errico

What purpose does this serve given the existence of cell2mat already in matlab? Also, this is limited to 1 or 2-d arrays, whereas cell2mat is not.

Finally, there is no error checking. You might argue that this (doubly looped) code checks for whether elements of the cell array are numeric or not, inserting NaNs there. But it still fails when the cell array elements are not scalar.

MATLAB Release
MATLAB 7.4 (R2007a)
Acknowledgements

Inspired: CELL2FLOAT, Rosin-Rammler Diagram plotting tool

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video