View License

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

» Watch video

Highlights from

4.7 | 3 ratings Rate this file 7 Downloads (last 30 days) File Size: 2.26 KB File ID: #19730 Version: 1.3



Jos (10584) (view profile)


27 Apr 2008 (Updated )

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

| Watch this File

File Information

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


Cell2num inspired this file.

This file inspired Pivot/Un Pivot.

MATLAB release MATLAB 6.5 (R13)
Other requirements should work in most ML releases
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
02 Jun 2010 Ian Howat

Ian Howat (view profile)

30 Dec 2009 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.

Comment only
03 Dec 2009 Oleg Komarov

Oleg Komarov (view profile)

03 Dec 2009 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.

Comment only
03 Dec 2009 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.

Comment only
22 Sep 2008 A K

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!

29 Apr 2008

spelling errors

04 Dec 2009 1.1

incorporated the error suggestion by Oleg

07 Dec 2009 1.2

updated description

04 Jan 2010 1.3

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

Contact us