File Exchange

image thumbnail


version 1.3 (2.26 KB) by

converts cell array into scalar float array (v4.0, jan 2010)

3 Ratings



View License

CELL2FLOAT - converts cell array into scalar float array

M = CELL2FLOAT(C) returns a float array M with the same size as the cell array C. C can be any N-dimensional cell array. Cells of C that contain a scalar float (single or double) are put in corresponding locations of M. Cells that contain other datatypes (e.g., strings, arrays, empty, integers, etc.) yield NaNs in the corresponding locations of M.

M = CELL2FLOAT(C,F) uses the value F instead of NaN to fill these latter locations. F should be a scalar float (single or double).

M = CELL2FLOAT(C,F,'error') will cause the program to error if a cell does not contain a scalar float. If F is empty ([]), NaN will be used as the filler value.

[M,Q] = CELL2FLOAT(C, ..) returns a logical array Q with logical ones (true) where the values of C are floats, and logical zeros (false) elsewhere.

C = {single(1) 2 'x' [] ; 1:3 complex(1,2) uint8(1) Inf}
M = cell2float(C)
% -> [ 1.00 2.00 NaN NaN ;
% NaN 1.00 + 2.00i NaN Inf ]

cell2float({1 2 [] 3 [] 5},999)
% -> [ 1 2 999 3 999 5 ]

% ??? Error using ==> cell2float at 80
% Not all cells contain a scalar float.

See also cell2mat, cellfun, num2cell

version 4.0 jan 2010

Comments and Ratings (6)

Ian Howat

Ian Howat (view profile)

Oleg Komarov

Oleg Komarov (view profile)

>> cell2float({23,35},'error')
ans =

line 64 isn't consistent with char preallocation.

I substituted line 57 with:
if nnz(~cellfun('isclass',C,class(C{1}))) > 0
error('cell2float:NonScalerCells','Not all cells contain a scalar float.');
else Filler = NaN;

Then I deleted line 70-72.

Oleg Komarov

Oleg Komarov (view profile)

Oleg Komarov

Oleg Komarov (view profile)

I added on line 49 (shifting the rest down) these lines of code:
if nnz(~cellfun('isclass',C,class(C{1}))) > 0
'Filler not supplied. All contents of C must be of the same data type.')

I'll add your cell2float (modified with those lines) + license to my next submission (instead of using cell2mat) if you agree.

Oleg Komarov

Oleg Komarov (view profile)

I would prefer the default behaviour to throw an error if a mixed datatype cell array is supplied.
Its auto-NaN behavior can pass unobserved and can lead to late detection of the error if the input was meant to be a cell array of scalars n the first place.
This means that if i believe my Input is a cell array of scalars but it is not i will notice only by supplying the index output of cell2float and calling an any(IDX).

So i'd like a more transparent syntax as:
cell2float(In) --> error if In mixed datatypes.


Works the magic in a short time. I needed to convert a cell vector which had numerous empty cells and a few sparse 1's into an equivalent vector with 1's and 0's. I had not managed to find a fast, vectorized approach until now. Thanks!



fixed error noted by Oleg. Note that the syntax has changed.


updated description


incorporated the error suggestion by Oleg

spelling errors

MATLAB Release
MATLAB 6.5 (R13)

Inspired by: cell2num

Inspired: Pivot/unPivot

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

» Watch video

Win prizes and improve your MATLAB skills

Play today